Sk8eR
Sk8eR

Reputation: 63

Android - Gridview updating adapter on background

i have an AsyncTask where in "doinbackground", it update the variable "pics", later in postexecute i update all, the problem is than i wanna update the adapter when i'm updating the varible "pics". where i should declare the adapter, and call notifyDataSetChanged??

     protected void onPostExecute(String file_url) {
                 // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    mAdapter = new Gridadapter(tab.this, pics);

                       gridView.setAdapter(mAdapter);
                }
            });

thx!

Upvotes: 0

Views: 1114

Answers (1)

carlo.marinangeli
carlo.marinangeli

Reputation: 3038

you don't need to call the runOnUiThread(...) inside the onPostExecute. that method is already called inside the UI thread.

the adapter can be declared when you declare the other components of the views and you should use always the same instance. (dont create a new adapter every time you have an update do do!)

I would create an adapter like that:

public class GridAdapter extends BaseAdapter{

private ArrayList<Items> mItemList;

    public void updateItemList(ArrayList<Items> newItemList){
        this.mItemList = newItemList;
        notifyDataSetChanged();
    }

}

then instance it:

public void onCreate(Bundle savedInstance){
    // ...all the previous code

    mGridView = (GridView) findViewById(R.id.gridview);
    mGridAdapter = new GridAdapter(this);
    mGridView.setAdapter(mGridAdapter);

}

and call the update from the onPostExecute:

protected void onPostExecute(String file_url) {
    mGridAdapter.updateItemList(pics);

}

Upvotes: 1

Related Questions