每次数据更改时更新ListView [英] Update ListView everytime the Data changes

查看:93
本文介绍了每次数据更改时更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和Java建立聊天,正在使用Json文件从聊天中获取数据,并插入调用PHP文件的Data,该文件将新消息插入数据库.

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.

我正在列出Listview中的所有消息,并且每次发送新消息时我都尝试更新listview,我的代码正在运行,但是我想在出现以下情况时自动更新Listview我发送新消息.

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.

我必须再次单击聊天室才能看到Listview的更新.我想同时更新接收者和发送者两个用户的listview.

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.

我尝试使用以下方法更新适配器:notifyDataSetChanged();,但是它不起作用.

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

我该怎么做?

代码:

我的适配器:

@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);

}

课程

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;  

   }

谢谢.

推荐答案

您需要使用 CursorLoader ,它包装了 ContentObserver ,它订阅了 ContentProvider中的更改(按特定URI).

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

您的聊天应用程序应包含ContentProvider以与数据库一起使用,CursorLoader可以加载您的聊天记录并在更改的数据库数据上更新UI ...

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

这篇关于每次数据更改时更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆