2

I'm trying to add a chat feature in my Android app using Parse.com. I'm using a custom 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
  • When I send a message, I create a new ParseObject (message in my database) and I save it to the conversation ParseObject.
  • I save the message, then I save the conversation, both with saveInBackground(new SaveCallback() {...}) and 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...

I noticed that the getCount() method always return what is displayed.

Any idea of how I could refresh my listView's data ?

Thanks !

Nebneb
  • 114
  • 8

2 Answers2

0

The problem was that I get the list of message in a conversation in the constructor of the parseQueryAdapter and then return the query. The parseQueryAdapter works well, displaying me the result of the query. 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. It works perfectly now !

Nebneb
  • 114
  • 8
  • Hi, I've the same problem, can you explain better (maybe with an example) how you have solved it? Thank you – Kurtis92 Mar 23 '15 at 20:04
0

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. Now it is just a simple parse query adapter:

ParseQuery<ParseObject> q = new ParseQuery<ParseObject>("Message");
q.whereEqualTo("conversationPointer", parseConversationId);
Nebneb
  • 114
  • 8
  • So...should I not use ParseQueryAdapter? – Kurtis92 Mar 30 '15 at 20:21
  • You should use it because it is very convenient. You have easy pagination, updates for you data... The only condition to use it is that you can get what you have to display by a simple query. ParseQueryAdapter is a good way to display the result of a query as a list. – Nebneb Apr 01 '15 at 10:49