3

I am developping a launcher with two different profiles : one is the main one the other is secondary. If I go on the play store and then come back with the "back" button, I land on my main profile, but there, the icons of my gridview (displayed through an adapter) appear in double (if I install two apps from the play store, coming back with the back button will give me icons 3 times and so on).

If I restart my main activity in the onresume method, things go fine and my icons aren't doubled, or tripled or whaterver. But I don't want to restart the activity, I only want to empty my gridview.

if((admin == true) ){
        Intent intent = getIntent();
        finish();
        startActivity(intent);}
Zizou
  • 1,891
  • 3
  • 15
  • 16

3 Answers3

11

I had the same problem myself. The easiest way to do this is:

gridview.setAdapter(null);
Bogdan Alexandru
  • 5,394
  • 6
  • 34
  • 54
3

you can put your data to be loaded with gridview into a list, then when you want to update your gridview ,just use

    list.clear();

then reload your updated data to your list,set adapter again.Here below shows my code:

I use gridview to load images from web,fetched by Asynctask;when I want to update those images when back from setting to my Fragment/Activity, I clear the list containing data and refilling work is done in AsyncTask.

    public void onStart() {
    super.onStart();
    list.clear();
    //setAdapter(null) is not useful here after testing.
    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(getActivity());
    String sort_order=preferences.getString("pref_sortOrder","popular");
    ParseDataTask parseDataTask=new ParseDataTask();
    parseDataTask.execute(sort_order);
    }

hope it helps :)

0

Remove items from adapter of the GridView. You can obtain adapter by calling #getAdapter() You may need to implement cleaning method in adapter if it not exist. If adapter extends BaseAdapter you may need to call #notifyDataSetChanged()

koral
  • 2,513
  • 1
  • 32
  • 41