Custom ListView is not updated when items are inserted
<p>I have a custom listview with custom adapter extending BaseAdapter if i add items to this list view in OnCreate method they show up in list, but if i add them from other methods like a packet listener method then items do not show up , on the screen below this listview there is a textbox if i select textbox to entertext using virtual keyboard immediately the listview gets populated with previousely inserted items which didnt show up. This activity is a chat window basically
I have tried calling notifyDataSetChanged, invalidate on Layout or on listview but nothing helped.</p>

<p>What i think is i need to have a way to refresh activity , as same thing must be happening when the virtual keyboard pops up .</p>

<p>Help will be highly appreciated</p>

<p>Thanks</p>

<p>Code:</p>

<p>package com.arounds;</p>

<p>public class ChatActivity extends Activity implements OnClickListener,PacketListener{</p>

<pre><code> private ListView chatView;  
 private ChatListViewCustomAdapter adapter; 
 private String user;
 private XMPPConnection connection;
 private Conversation conv;
 private ChatActivity selfRef = this;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.chat_win);

        AroundApplication app = (AroundApplication) this.getApplicationContext();
        connection = app.getConnection();

        chatView = (ListView) findViewById(R.id.conversationList);  
        adapter = new ChatListViewCustomAdapter(this);  
        chatView.setAdapter(adapter); 

        // set send btn listener
        ImageButton send = (ImageButton)findViewById(R.id.imgBtnSend);
        send.setOnClickListener(this);

        ImageButton smiley = (ImageButton)findViewById(R.id.imgBtnSmiley);
        smiley.setOnClickListener(this);
        // get the parameter passed by previouse activity

        Bundle b = this.getIntent().getExtras();

        String temp = b.getString("user");
        user = temp;

        TextView v = (TextView)this.findViewById(R.id.txtViewTitle_chat);
        v.setText(temp);

        v = (TextView)this.findViewById(R.id.txtViewDescription_chat);
        temp = b.getString("status");
        v.setText(temp);
        //chatView.setOnItemClickListener(this);
        HashMap convs = app.getConversations();

        if(convs.containsKey(user) == true)
            conv = (Conversation) convs.get(user);
        else {
            conv = new Conversation();
            convs.put(user,conv);
        }

        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(this,filter);


 }


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId() == R.id.imgBtnSend)
    {
    EditText msg = (EditText)this.findViewById(R.id.editChat);
    String s = msg.getText().toString();
     Message message = new Message(user, Message.Type.chat);
     message.setBody(s);
     connection.sendPacket(message);

     ArrayList&lt;ChatMessage&gt; m = conv.messages;
     String currentDate = DateFormat.getDateInstance().format(new Date()); 

     m.add(new ChatMessage(s,currentDate));

     adapter.addItem("I said",s,currentDate,Constants.SEND_LIST_TYPE);
     //adapter.notifyDataSetChanged();
    }
    else
    {
        //View view = this.findViewById(R.id.linerLayoutChat);
        chatView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}


@Override
public void processPacket(Packet packet) {
    // TODO Auto-generated method stub
    System.out.println("in");
      Message message = (Message) packet;
      if (message.getBody() != null) {

          System.out.println("in1");
            String fromName = StringUtils.parseBareAddress(message.getFrom());
            ArrayList&lt;ChatMessage&gt; m = conv.messages;
            String currentDate = DateFormat.getDateInstance().format(new Date()); 

            m.add(new ChatMessage(message.getBody(),currentDate));

            adapter.addItem(fromName+" said",message.getBody(),currentDate,Constants.REC_LIST_TYPE);

            //chatView.postInvalidate();
      }
}
</code></pre>

<p>}</p>

<p>Adapter class:</p>

<pre><code>public class ChatListViewCustomAdapter extends BaseAdapter
 {
public ArrayList&lt;ChatListItem&gt; items;
public Activity context;
public LayoutInflater inflater;
public Boolean temp=false;

public ChatListViewCustomAdapter(Activity context) {
    super();

    this.context = context;
    this.items = new ArrayList&lt;ChatListItem&gt;();
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder
{

    TextView txtViewTitle;
    TextView txtViewDescription;
    TextView txtViewDate;
}
public void addItem(String title,String desc,String d,int type)
{
    ChatListItem item = new ChatListItem(title,desc,d,type);
    items.add(item);
    notifyDataSetChanged();


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ChatListItem item = items.get(position);
    ViewHolder holder;

    System.out.println("Title:"+item.title+" type:"+item.type);
    if(convertView==null)
    {
        holder = new ViewHolder();

        int type = this.getItemViewType(position);
        if(type == 0)
        {
            convertView = inflater.inflate(R.layout.list_item_even, null);
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitleEven);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescriptionEven);
            holder.txtViewDate = (TextView) convertView.findViewById(R.id.txtViewDateEven);

        }
        else
        {
            convertView = inflater.inflate(R.layout.list_item_odd, null);
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitleOdd);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescriptionOdd);
            holder.txtViewDate = (TextView) convertView.findViewById(R.id.txtViewDateOdd);

        }


        convertView.setTag(holder);

    }
    else
        holder=(ViewHolder)convertView.getTag();


    holder.txtViewTitle.setText(item.title);
    holder.txtViewDescription.setText(item.desc);
    holder.txtViewDate.setText(item.date);

    return convertView;
} 
 @Override
 public int getItemViewType(int position) {
     ChatListItem item = items.get(position);
     return item.type;
 }
 @Override
  public int getViewTypeCount() {
      return 2;
  }
</code></pre>

<p>}</p>
