简体   繁体   English

使用parseQueryAdapter的Android Parse.com聊天不会刷新

[英]Android Parse.com Chat with parseQueryAdapter does not refresh

I'm trying to add a chat feature in my Android app using Parse.com. 我正在尝试使用Parse.com在我的Android应用程序中添加聊天功能。 I'm using a custom parseQueryAdapter . 我正在使用自定义parseQueryAdapter Problem is that when I send a message or when I receive a new message, my adapter does not update. 问题是当我发送消息或收到新消息时,我的适配器不会更新。 I tried to add both 我试图将两者都添加

adapter.notifyDataSetInvalidated();
adapter.clear();
adapter.loadObjects();
adapter.notifyDataSetChanged();

in done callback and even in a dedicated refresh button. 完成回调,甚至在专用的刷新按钮中。

This is how it works : 这是这样的:

  • My adapter is set to a custom listView and a query is set in adapter's constructor 我的适配器设置为自定义listView,并且在适配器的构造函数中设置了查询
  • When I send a message, I create a new ParseObject (message in my database) and I save it to the conversation ParseObject. 发送消息时,我创建了一个新的ParseObject(数据库中的消息),并将其保存到对话ParseObject中。
  • I save the message, then I save the conversation, both with saveInBackground(new SaveCallback() {...}) and saveEventually(new SaveCallback() {...}) 我保存消息,然后保存对话,都使用saveInBackground(new SaveCallback(){...})saveEventually(new SaveCallback(){...})
  • I try to refresh in the done callback 我尝试刷新完成的回调

Each time, the listView displays same messages, and I have to come back to the previous fragment and re-open the chat fragment to see new messages... 每次,listView显示相同的消息,我必须回到上一个片段,然后重新打开聊天片段以查看新消息...

I noticed that the getCount() method always return what is displayed. 我注意到getCount()方法总是返回所显示的内容。

Any idea of how I could refresh my listView's data ? 关于如何刷新listView数据的任何想法?

Thanks ! 谢谢 !

The problem was that I get the list of message in a conversation in the constructor of the parseQueryAdapter and then return the query. 问题是我在parseQueryAdapter的构造函数中的对话中获取消息列表,然后返回查询。 The parseQueryAdapter works well, displaying me the result of the query. parseQueryAdapter运作良好,向我显示了查询结果。 If I send a message in a conversation, it has no effect on the list made in the constructor... 如果我在对话中发送消息,则它对构造函数中创建的列表没有影响...

So I added a column in my message ParseObject, and do a simple whereEqualTo() to have the right messages. 因此,我在消息ParseObject中添加了一个列,并执行了一个简单的whereEqualTo()以获取正确的消息。 It works perfectly now ! 现在,它可以完美运行!

Here are few more explanations The problem came from the adapter lifecycle. 以下是更多解释。问题来自适配器的生命周期。 I had : 我有:

constructor {
  set up the messages list
  set this list as source of data in query
  ...
}

when I tried to refresh, data were updated, but the list is still the same , so I got the same result ! 当我尝试刷新时,数据已更新, 但列表仍然相同 ,因此得到的结果相同! As it is not possible to change the query of a existing parseQueryAdapter, I completely changed the way I get messages, with a conversation pointer in my parse object. 由于无法更改现有parseQueryAdapter的查询,因此我完全更改了获取消息的方式,在parse对象中使用了会话指针。 Now it is just a simple parse query adapter: 现在,它只是一个简单的解析查询适配器:

ParseQuery<ParseObject> q = new ParseQuery<ParseObject>("Message");
q.whereEqualTo("conversationPointer", parseConversationId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM