How do I automatically refresh a GalleryView from a handler called by doInBackground?

StackOverflow https://stackoverflow.com/questions/8144631

  •  01-03-2021
  •  | 
  •  

سؤال

I am trying to refresh a GalleryView with new items by adding the items to the Adapter and doing the following inside the handler which is called from inside doInBackground method of AsyncTask.

private final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        if(msg.arg1 == 1){

            gaAdapter.notifyDataSetChanged();
            ga.setAdapter(gaAdapter);
            ga.setSelection(midposition);
        }
    }   
};

The GalleryView seems to respond to the above code but does not automatically refresh itself. The items are updated only when it is horizontally scrolled. How can I do this automatically?

هل كانت مفيدة؟

المحلول

notifyDataChanged() need to be involved on UI thread, try this:

runOnUiThread(new Runnable() {
  public void run() {
    gaAdapter.add(newListItem);
    gaAdapter.notifyDataSetChanged();
    ga.setSelection(midposition);
  }
});

نصائح أخرى

Try using
ga.invalidate()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top