Java tutorial
package net.thetabx.gcd.activity; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.util.ArrayMap; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.android.volley.Response; import com.android.volley.VolleyError; import net.thetabx.gcd.GCD; import net.thetabx.gcd.R; import net.thetabx.gcd.objects.ForumUser; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by ZergRael on 30/10/2014. * Under MIT license (See LICENSE) */ public class ChatActivity extends Activity { private List<ChatMessage> messages; private final String LOG_TAG = "ChatActivity"; private TextView header; private SwipeRefreshLayout swipe; private ListView listView; private ChatRowAdapter adapter; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); header = (TextView) findViewById(R.id.txt_chat_header); listView = (ListView) findViewById(R.id.lview_chat); swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_chat); swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { fetchFreshData(); } }); edit = (EditText) findViewById(R.id.etxt_chat_send); edit.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { header.setText(String.format("Sent %s", v.getText())); sendChatMessage(v); //v.setText(""); fetchFreshData(); } return false; } }); header.setText("Start"); if (messages == null) { fetchFreshData(); } else fillActivity(); } private void sendChatMessage(final TextView v) { Uri.Builder uri = new Uri.Builder(); uri.appendPath("forums").appendPath("mchat.php"); header.setText("fetchFreshData"); Map<String, String> params = new HashMap<>(); //TODO: sendChatMessage params //v.getText(); GCD.getHttp().post(uri, params, new Response.Listener<String>() { @Override public void onResponse(String response) { if (GCD.getHttp().isLoggedIn()) { v.setText(""); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); } private void fetchFreshData() { swipe.setRefreshing(true); Uri.Builder uri = new Uri.Builder(); uri.appendPath("forums").appendPath("mchat.php"); header.setText("fetchFreshData"); GCD.getHttp().get(uri, new Response.Listener<String>() { @Override public void onResponse(String response) { if (GCD.getHttp().isLoggedIn()) { header.setText("Got data"); parse(response); fillActivity(); swipe.setRefreshing(false); } else finish(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { header.setText("Failed volley"); swipe.setRefreshing(false); } }); } private void parse(String response) { Document html = Jsoup.parse(response); Elements blocks = html.select(".mChatHover>div"); messages = new ArrayList<ChatMessage>(); for (Element block : blocks) { messages.add(new ChatMessage(block.select("a").get(0), block.select("span").get(0), block.select("div").get(1))); } header.setText(String.format("Got %d messages", messages.size())); } private void fillActivity() { if (adapter == null) adapter = new ChatRowAdapter(messages); listView.setAdapter(adapter); } public class ChatMessage { public ForumUser forumUser; public String date; public String content; public ChatMessage(Element user, Element date, Element content) { forumUser = new ForumUser(); forumUser.name = user.text(); String href = user.attr("href"); forumUser.matchId(href); if (user.hasClass("username-coloured")) { forumUser.matchColor(user.attr("style")); } else forumUser.color = Color.BLACK; this.date = date.text().trim().substring(2); this.content = content.text(); } } private class ChatRowAdapter extends ArrayAdapter<String> { private List<ChatMessage> data; private LayoutInflater inflater; public ChatRowAdapter(List<ChatMessage> data) { super(ChatActivity.this, R.layout.row_chat); this.data = data; inflater = getLayoutInflater(); } @Override public int getCount() { return data.size(); } @Override public View getView(int pos, View convertView, ViewGroup parent) { ChatRowViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.row_chat, parent, false); holder = new ChatRowViewHolder(convertView); convertView.setTag(holder); } else holder = (ChatRowViewHolder) convertView.getTag(); ChatMessage msg = data.get(pos); holder.getUsernameText().setText(msg.forumUser.name); holder.getUsernameText().setTextColor(msg.forumUser.color); holder.getDateText().setText(msg.date); holder.getContentText().setText(msg.content); return convertView; } } public class ChatRowViewHolder { private View row; private TextView username = null, date = null, content = null; public ChatRowViewHolder(View row) { this.row = row; } public TextView getUsernameText() { if (this.username == null) this.username = (TextView) row.findViewById(R.id.txt_rowchat_username); return this.username; } public TextView getDateText() { if (this.date == null) this.date = (TextView) row.findViewById(R.id.txt_rowchat_date); return this.date; } public TextView getContentText() { if (this.content == null) this.content = (TextView) row.findViewById(R.id.txt_rowchat_content); return this.content; } } }