1

There is a custom ListView which populate the data from server using PHP and it is working fine. I have a notifyDataSetChanged for list update. But the problem is when there is a change in data on server i need to press back button to see the updated data in the ListView. Is there any better way to show the updated list which is similar to WhatsApp list.

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seconds);

    contlist = (ListView)findViewById(R.id.contlist);
    startService(new Intent(this, serv.class));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if(!prefs.getBoolean("firstTime", false)) {
        try {
            getNumber(seconds.this.getContentResolver());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("firstTime", true);
        editor.commit();
    } else {
        musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
        descAdapter = new DescAdapter(seconds.this, musers, seconds.this);
        contlist.setAdapter(descAdapter);
     }

    intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_TIME_TICK);

    seconds.this.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            descAdapter.notifyDataSetChanged();
        }
    }, intentFilter);
}

I am using intentfilter in OnCreate method for notifyDataSetChanged. I placed the intent filter code in the OnStart, OnResume method also but I need to click back button in order to see the updated list. Any better way to show updated list in realtime ?

user2269164
  • 1,095
  • 2
  • 15
  • 31

1 Answers1

0

You must need to refresh the data everytime if some changes happens at the server. But you can implement Swipe to Refresh pattern to achieve it without having to press the back button.

I suggest you to go to this link A basic example of it!

android_griezmann
  • 3,757
  • 4
  • 16
  • 43