1

I am using loaders to load data into a gridview . First let me show you some code:-

The Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle icic){

    View v=inflater.inflate(R.layout.thegrid, container, false);
    final GridView gv = (GridView)v.findViewById(
            R.id.thegridview);
    gv.setAdapter(upAd);//upAd is the array adapter initialized in oncreate of the fragment

    gv.setOnItemClickListener(this);
    Bundle args=new Bundle();
    args.putString("page", String.valueOf(page));
    getLoaderManager().initLoader(1, args, this);

    return v;
}
@Override
public Loader<ArrayList<HashMap<String, String>>> onCreateLoader(int loaderId,Bundle thebundle) {

    if(loaderId==1){
        String page=thebundle.getString("page");
        return new MyLoader(getActivity().getApplicationContext(),page);
    }

    return null;
}


@Override
public void onLoadFinished(Loader<ArrayList<HashMap<String, String>>> theLoader,ArrayList<HashMap<String, String>> data) {
    upAd.addAll(data);
}

Now when the fragment is displayed , it shows a grid of 10 elements downloaded.Now when the screen orientation is changed, the loader behaves well and does not download the data again but it adds the same data again to the grid.. so i end up having a grid of 20 elements the same 10 repeated twice. Well i can call :-

upAd.clear() 

in onLoadFinished but i want to avoid that.Could someone tell me what would I be doing wrong here.

Rasmus
  • 8,248
  • 12
  • 48
  • 72

2 Answers2

0

You have 20 items because ArrayList holding data in upAd already has 10 items from past. Then you are calling upAd.addAll(data);. That means you are adding another 10 identical items to that array list. So the solution is to clear adapter.

traninho
  • 1,533
  • 12
  • 19
  • that is what i meant when i said I can use upAd.clear() but wanted to avoid that, but it seems that might be the only way out here – Rasmus Mar 06 '13 at 11:35
-1

use this line in your Activity declaration in menifiest file.

android:configChanges="orientation|keyboardHidden"
Kamal
  • 1,415
  • 13
  • 24
  • 3
    Perhaps you would want to have a link at this url http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation – Rasmus Aug 18 '12 at 11:51