I am trying to use runnable method from Android os to update a chat.
My problem is runnable does not work. For example,
I have the following messages:
Joe: hello
Jack: hi
The chat is asynchronous, so both can see.
Here is my code:
Runnable mUpdater = new Runnable() {
@Override
public void run() {
// check if still in focus
if (!mRunning) return;
// upadte your list view
Firebase f_reply = new Firebase("https://myapp.firebaseio.com/Replies);
f_reply.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snap_reply) {
Iterable<DataSnapshot> rs = snap_reply.getChildren();
Iterator<DataSnapshot> irs = rs.iterator();
long allNum2 = snap_reply.getChildrenCount();
int maxNum2 = (int)allNum2;
if(irs.hasNext())
{
replyAdapter disadpt = new replyAdapter(getMsgActivity.this,reply_msg_list);
if(curr_count <= maxNum2)
{
Map<String, Object> nPost = (Map<String, Object>) irs.next().getValue();
String reply_msg = nPost.get("repMsg").toString();
String reply_msg_id = nPost.get("msgid").toString();
if(reply_msg_id.equals(msgID))
{
System.out.println(reply_msg);
reply_msg_list.add(reply_msg);
}
reply_list.setAdapter(disadpt);
}
curr_count++;
}
}
@Override
public void onCancelled(FirebaseError arg0) {
// TODO Auto-generated method stub
}
});
// schedule next run
mHandler.postDelayed(this, 500); // set time here to refresh views
}
};
note:
curr_count is the number of messages already displayed.
So every 500 ms this runnable method will refresh, checking curr_count and see if it is less than or equal to the maximum amount of replies (maxNum2) in the fire base replies database.
If there is a new message added, set it into the adapter (display it) and add 1 to curr_count.
My problem is that the runnable method is not reloading (refresh), it is doing nothing at all at the moment.
Can someone have a better solution than using runnable or tell me where I went wrong?