How to show chats by collecting messages in the custom list view adapter itself?
<p>So, What I want to do is display chats in my activity using a custom list view adapter.</p>
<blockquote>
<p>I have a HTTPTask Activity handling the server side interaction and responding with a JSONObject. So, every server side interaction is working fine.</p>
<p>What I want to do is keep updating the messages in the chat by keep checking with the API at a set interval to populate messages in the chat if there are any.</p>
</blockquote>
<p><strong>My question is, should this population process done in the adapter or the activity and how?</strong></p>
<p><strong>And, how does viewHolder help in the adapter?</strong></p>
<p><strong>This is my Activity</strong></p>
<pre><code>public class ChatActivity extends Activity {

TextView toUsername;
EditText replyText;
JSONObject resultObject;
StringBuilder reply,from_user_id,c_id;
MessageListViewAdapter myAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    toUsername = (TextView) findViewById(R.id.toUsername);
    replyText = (EditText) findViewById(R.id.replyText);
    reply = new StringBuilder(&quot;&quot;);
    listView = (ListView) findViewById(R.id.messages);
}

@Override
public void onResume(){
    super.onResume();

    Bundle bundle = getIntent().getExtras();
    if(bundle != null){
        toUsername.setText(&quot;&quot; + bundle.get(&quot;ToUsername&quot;).toString());
        c_id = new StringBuilder(bundle.get(&quot;c_id&quot;).toString());
        from_user_id = new StringBuilder(bundle.get(&quot;FromUserId&quot;).toString());
    }

    myAdapter = new MessageListViewAdapter(getBaseContext(),c_id.toString(),from_user_id.toString());
    listView.setAdapter(myAdapter);
}

public void sendTextMsg(View view){
    reply.delete(0,reply.length());
    reply.append(replyText.getText().toString());
    if(!reply.toString().equals(&quot;&quot;)){
        Log.d(&quot;Values: &quot;,&quot;c_id: &quot; + c_id.toString() + &quot; FromUserId: &quot; + from_user_id.toString() + &quot;ReplyText: &quot; + reply.toString());

        try{
            resultObject = new HttpTask(getBaseContext()).doInBackground(&quot;replyInChat&quot;,c_id.toString(),replyText.getText().toString(),from_user_id.toString());
            if(resultObject.get(&quot;status&quot;).toString().equals(&quot;true&quot;)) {
                Toast.makeText(getBaseContext(), &quot;Sent.&quot;, Toast.LENGTH_SHORT).show();
                replyText.setText(&quot;&quot;);
            }
            else {
                Toast.makeText(getBaseContext(), &quot;Try Again.&quot;, Toast.LENGTH_SHORT).show();
            }
        }
        catch(JSONException e){ }

    }
}
}
</code></pre>
<p><strong>My Adapter doesn't seem to work.</strong></p>
<pre><code>public class MessageListViewAdapter extends BaseAdapter implements ListAdapter{

private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
private Context context;
private StringBuilder conversation_id, user_id;
private static int cr_id;
private JSONArray messages;
private JSONObject resultObject;
private ViewHolder viewHolder;
private View rowView;

public MessageListViewAdapter(Context context, String conversation_id, String user_id) {
    this.context = context;
    this.conversation_id = new StringBuilder(conversation_id.toString());
    this.user_id = new StringBuilder(user_id.toString());
    cr_id=0;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int pos) {
    return list.get(pos);
}

@Override
public long getItemId(int pos) {
    //return list.get(pos).getId();
    //just return 0 if your list items do not have an Id variable.
    return 0;
}

@Override
public boolean isEnabled(int position){
    return false;
}

static class ViewHolder{
    public TextView ItemText;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    rowView = convertView;

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.message_list_layout, null);
        //configure view holder
        viewHolder = new ViewHolder();
        viewHolder.ItemText = (TextView) rowView.findViewById(R.id.list_item_text);
        rowView.setTag(viewHolder);
    }
    else {
        //fill data
        viewHolder = (ViewHolder) rowView.getTag();
    }

    try{
        Log.d(&quot;cr_id: &quot;,String.valueOf(cr_id).toString());
        //This is where the population should've taken place but didn't.
        resultObject = new HttpTask(context).doInBackground(&quot;sendMessages&quot;,conversation_id.toString(),String.valueOf(cr_id));
        if(resultObject.get(&quot;status&quot;).toString().equals(&quot;true&quot;)) {
            messages = resultObject.getJSONArray(&quot;messages&quot;);
            Log.d(&quot;Messages: &quot;,messages.toString());
            for(int i=0;i&lt;=messages.length();i++){
                list.add(messages.getJSONObject(i).get(&quot;reply_text&quot;).toString());
            }
        }
    }
    catch(JSONException e){ }

    //Handle TextView and display string from your list
    //final TextView listItemText = (TextView)rowView.findViewById(R.id.list_item_text);
    //listItemText.setText(list.get(position));
    viewHolder.ItemText.setText(list.get(position));

    return rowView;
}
}
</code></pre>