首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每次数据更改时更新ListView

每次数据更改时更新ListView
EN

Stack Overflow用户
提问于 2015-04-29 17:02:12
回答 2查看 2.4K关注 0票数 0

我正在使用PHP和Java构建聊天,我使用Json文件从聊天中获取数据,并将调用PHP文件的数据插入到我的数据库中。

我列出了Listview中的所有消息,并且每次发送新消息时都尝试更新listview,我的代码正在工作,但是我想在发送新消息时自动更新Listview

我不得不再次点击聊天室来查看更新的Listview。我想从用户、接收方和发送方更新listview

我试图使用:notifyDataSetChanged();更新我的适配器,但是它没有工作。

我怎么能这么做?

代码:

My适配器:

代码语言:javascript
复制
@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);

}

代码语言:javascript
复制
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;  

   }

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-04-29 17:47:22

您一定要使用adapter.notifyDataSetChanged();。因此,您只需创建一个listener并让Listview实现它。在这个listener中,你把adapter.notifyDataSetChanged();。然后,当每个设备接收到信息时,只需调用此方法即可。

票数 0
EN

Stack Overflow用户

发布于 2015-04-29 17:52:54

您需要使用ContentObserver,CursorLoader,它封装了按特定URI订阅ContentProvider更改的

所以..。

您的聊天应用程序应该包含ContentProvider与DB一起工作,CursorLoader加载您的聊天历史记录并更新数据库数据上的UI。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29950092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档