ultrah
ultrah

Reputation: 1

Better Way to refresh ListView?

I have a Activty containing a custom ListView filled by Baseadapter.The Data displayed by this ListView changes all the time, so I need to refresh the List.

I do this by periodially calling notifyDataSetChanged() on my ListViewAdapter in a Thread and setting thread_paused to true if the activity is paused. Ugly as hell.

public void StartListener(){
  new Thread(new Runnable() {
      public void run() {
          while(!thread_paused){
              try {
                 Thread.sleep(5000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              runOnUiThread(new Runnable() {
                 public void run() {
                    ListViewA.notifyDataSetChanged();
                    Log.v("refresh", "refreshed view");
             }
        });
      }
     } 
  }).start();
}

@Override
public void onPause(){
  super.onPause();
  thread_paused=true;
}

I know there has to be a a better way to do this, maybe some kind of listener on the datasource ?

Any suggestions ?

Upvotes: 0

Views: 404

Answers (2)

ultrah
ultrah

Reputation: 1

solved it using a broadcastreceiver

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57672

Easy answer: Just update the list when the data of the list changed. Updating the list without that changed data makes no sense, so what ever changes the data should call notifyDataSetChanged()

Upvotes: 1

Related Questions