Update ListView everytime the Data changes

我的梦境 提交于 2019-12-25 03:37:11

问题


I'm building a Chat using PHP and Java, I'm getting the data from the chat using a Json file, and I am inserting the Data calling a PHP file that inserts a new message into my Database.

I'm listing all the messages in a Listview, and I am trying to update the listview everytime a new message is sent, my code is working, but I would like to automatically update the Listview when I send a new message.

I have to click at the chat room again to see the Listview updated. I would like to update the listview from both users, the receiver and the sender.

I tried to update my adapter using: notifyDataSetChanged(); but it didn't work.

How can i do that?

Code:

My adapter:

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);


    // Dismiss the progress dialog
    if (pDialog.isShowing())
        pDialog.dismiss();
    /**
     * Updating parsed JSON data into ListView
     * */


    adapter =  new costumeadapter(
                    ChatRoomActivity.this,
                    contactList,
                    R.layout.chat_row,
                    new String[]{ TAG_MESSAGE, TAG_CONVERSATION },
                    new int[]{ R.id.name, R.id.conversationid });

    setListAdapter(adapter);

}

Class

public View getView(int position, View convertView, ViewGroup parent){

      View v = super.getView(position, convertView, parent);


      TextView text = (TextView) v.getTag();

      if(text == null){
         text = (TextView) v.findViewById(R.id.name);
          v.setTag(text);
      } 

     String pos = (String) ((Map)getItem(position)).get(TAG_POSITION);

     text.setBackgroundResource(pos.equals("left") ? R.drawable.bubble_green : R.drawable.bubble_yellow);

     conversationid = (TextView)v.findViewById(R.id.conversationid);

     send.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

            String getmessage = txtmessage.getText().toString();
            String getconversationid = conversationid.getText().toString();

            SendMessage mTask = new SendMessage();
            mTask.execute(sessionid,getmessage,getconversationid);

            adapter.notifyDataSetChanged();
         }
     });

      return v;  

   }

Thanks.


回答1:


You definitely have to use adapter.notifyDataSetChanged();. So you just have to create a listener and make the Listview implements it. Into this listener you put adapter.notifyDataSetChanged();. Then you just call this method when each device receives the information




回答2:


You need to use CursorLoader, It wraps ContentObserver, which subscribes to changes in ContentProvider by specific URI.

So...

your chat app should contain ContentProvider to work with DB, CursorLoader that loads your chat history and updates UI on db data changed...



来源:https://stackoverflow.com/questions/29950092/update-listview-everytime-the-data-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!