1

This is the timer to refresh the listview to check for any new entry in sqlite. and whenever timer goes over, it refreshes the listview again and again. In that case, I want that the timer should have no effect on the listview. It should add the new entry in at the end without scrolling it down again again.

Timer t = new Timer();
                    t.scheduleAtFixedRate(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                public void run() {


                                    get_chat_data();

                                }
                            });
                        }
                    }, 500, 1000);

This is the function which is called in the timer

private void get_chat_data() {

      String fuid = getIntent().getExtras().getString("uid");
    HashMap<String, String> user = session.getUserDetails();
    String uid = user.get(SessionManager.KEY_UID);



            //  Fetching user details from sqlite
            db = new SQLiteHandler(getApplicationContext());
            /*
            *
            * sender chat
            *
            * */

            HashMap<String, String> user_info = db.CHAT_DATA_IN_LIST_VIEW(fuid, uid);

            String s_chat= user_info.get("s_chat");
            String mtime= user_info.get("mtime");
            String mdate= user_info.get("mdate");
            String spic= user_info.get("s_pic");


            String []  s_chat1 = s_chat.split(":");
            String []  mtime1 = mtime.split("=");
            String []  mdate1 = mdate.split(":");
            String []  spic1 = spic.split(":");


            /**
             *
             * reciver chat
             *
             * */

            HashMap<String, String> user_info1 = db.CHAT_DATA_IN_LIST_VIEW1(fuid, uid);
            String r_chat= user_info1.get("r_chat");
            String rpic= user_info1.get("r_pic");
            String friendid= user_info1.get("com_id");

            String []  r_chat1 = r_chat.split(":");
            String []  rpic1 = rpic.split(":");






            list.setAdapter(new Custom_Adapter_chat(this, s_chat1, r_chat1, spic1, rpic1, mtime1, mdate1));


        }
NightFury
  • 13,436
  • 6
  • 71
  • 120
DEEPAK MITTAL
  • 141
  • 1
  • 1
  • 10
  • Looks like you set a new adapter at the end of the `get_chat_data();` function. This would lose your list/recycler view's scroll state. So, get a reference to the original adapter, and try adding the new data using it. – C-- Oct 27 '15 at 04:57
  • Possible duplicate of [Listview Scroll to the end of the list after updating the list](http://stackoverflow.com/questions/3606530/listview-scroll-to-the-end-of-the-list-after-updating-the-list) – Himanshu Agarwal Oct 27 '15 at 04:58

1 Answers1

1

First of all, you are making a mistake by calling setAdapter again and again. You need to call notifyDataSetChanged() only to refresh the data in the list. And it will also position.

You should do something like this:

private void get_chat_data() {

       // your code to fetch records to fill in the array

      if(adapter==null)
      {
            adapter= new Custom_Adapter_chat(this, s_chat1, r_chat1, spic1, rpic1, mtime1, mdate1);
            list.setAdapter(adapter);
      }
      else
      {
           adapter.updateData(myNewData); //update your adapter's data
           adapter.notifyDatasetChanged();
      }

}
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • can i ask you where should i put this code i m not able to make it work @SweetWisher – DEEPAK MITTAL Oct 27 '15 at 06:55
  • You need to put it in `get_chat_data` method in place of line `list.setAdapter(new Custom_Adapter_chat(this, s_chat1, r_chat1, spic1, rpic1, mtime1, mdate1));` – SweetWisher ツ Oct 27 '15 at 09:03
  • heyy can i ask you how i m gonna add data to the listview here the code helped me great but i m not able to tranfer data to list view. help me @SweetWisher ツ – DEEPAK MITTAL Nov 04 '15 at 11:56