2

I'm doing GridView activity that first show default image, then i start a new thread for netowrk task to download image from datatbase, and i would like that after the thread is finished, the GridView will automatically refresh the images in grid.

from this question i took the following code:

ImageAdapter adapt = (ImageAdapter)gridView.getAdapter();
adapt.setBitmap(bitmaps);
adapt.notifyDataSetChanged();

which update the adapter of the grid. I'm doing this 3 lines inside the onResume() method but after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).

now if i'm moving to another acitivity (like one of the grid images) and then press the back button i can see the grid view image that i just downloaded from the database. (because it calls onPause() method and then onResume() )

Doe's anyone have a solution to this problem?

Thanks

Edit: The thread is running through AsyncTask

Community
  • 1
  • 1
Noam Mizrachi
  • 512
  • 2
  • 6
  • 15

1 Answers1

0

after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).

Rather than call onResume() by force, just move those three lines into a new method, call it refreshAdapter(). Then call refreshAdapter() inside onResume() and anywhere use you want to refresh the Adapter.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • 1
    It doesn't work. but the problem is that i'm using adapt.notifyDataSetChanged(); it execute from the second thread, which it can't update UI elements. and this method is working on UI. – Noam Mizrachi Jan 17 '13 at 17:06
  • 1
    @NoamMizrachi use runOnUiThread to do UI changes from a thread – K_Anas Jan 17 '13 at 17:11
  • Are you using a real Thread or an AsyncTask? If you're using a Thread, you could create a simple callback. – Sam Jan 17 '13 at 17:12
  • 1
    @NoamMizrachi I stumbled upon your edit: "The thread is running through AsyncTask". You cannot alter the UI in `doInBackground()` but you can in `onPostExecute()`. – Sam Jan 17 '13 at 19:14
  • 1
    Ok i will try this and let you know.. Tnx for the help! – Noam Mizrachi Jan 17 '13 at 19:45