1

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?

Community
  • 1
  • 1
nothingness
  • 694
  • 3
  • 9
  • 25
  • Runnable is an interface. You'll need to create a new Thread instance, that takes an instance of a Runnable in its constructor, then start the thread that way. – TEK Mar 29 '15 at 17:35
  • here http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android it doesn't use a thread, only a handler – nothingness Mar 29 '15 at 17:44
  • My mistake, I read this as a pure Java question. Sorry! – TEK Mar 29 '15 at 18:07
  • This is a question about Runnable, but Runnable doesn't appear anywhere in your code. You state that the problem is that it doesn't run every 500ms, but there is no code here that implies it would run every 500ms. – Kato Mar 31 '15 at 16:51
  • So I need code to start the runable? I dont know what should be use – nothingness Mar 31 '15 at 23:55
  • Why not use AsyncTask instead – mgokgoz Oct 01 '15 at 07:27

0 Answers0