Skip to content Skip to sidebar Skip to footer

Android - Having Trouble Updating A Listadapter After An Asynch Call

I am setting up a ListView adapter like this: public class SeeAllQuestionsActivity extends Activity { //ArrayAdapter adapter; SimpleAdapter mSchedule = n

Solution 1:

In your OnCreate you define a new SimpleAdapter and attach it to your ListView. This is correct. The datasource (mylist in your case) is empty at this point so you will fill it with in an AsyncTask.

In your onPostExecute you are creating a new ArrayList. Depending on the result you receive, you fill it. After you did this, you are setting the adapter again. Which will do nothing because the adapter has no data.. What you want to do, is give your new filled list to your Adapter so it can fill the ListView with your data.

Solution 1

onPostExecute {
   // create a list to store your datanewArrayList// fill the new list with the data you received (result)
   fillArrayList fromJSON// create an adapter and give the new list with it
   mAdapter = newSimpleAdapter(.., ArrayList, ...)
   listView.setAdapter(mAdapter);   
}

This is one way you can do it and fits you current implementation.

Solution 2

I would opt for this solution

onPostExecute {
    // don't create a new arraylist but use the mylist-object you created in the OnCreate
    fill mylist objectwithnew data 

    // mylist is the datasource of your adapter and it is now changed.// let the ListView know his adapter received new information
    mSchedule.notifyDataSetChanged
}

UPDATE

Check out this tutorial. I use the same layouts and same source to fill my list but I have altered it so it is similar to your case. Good luck :)

MainActivity

publicclassMainActivityextendsActivity{

    privateList<HashMap<String, String>> fillMaps;
    private SimpleAdapter adapter;

    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView) findViewById(R.id.listview);

        String[] from = newString[] { "rowid", "col_1", "col_2", "col_3" };
        int[] to = newint[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };

        // My data
        fillMaps = new ArrayList<HashMap<String, String>>();

        // Create an adapter which will tell my ListView what to show..// fillMaps is still empty so at the moment it my ListView will show// nothing.
        adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
        lv.setAdapter(adapter);

        // Retreive data from somewherenew UpdateListWithAsyncTask().execute();
    }

    privateclassUpdateListWithAsyncTaskextendsAsyncTask<Void, Void, Void> {
        protectedVoid doInBackground(Void... params) {
            // do stuffreturnnull;
        }

        protectedvoid onPostExecute(Void result) {
            // Fill your datasource that your adapter has. In my case fillMapsfor (int i = 0; i < 10; i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("rowid", "" + i);
                map.put("col_1", "col_1_item_" + i);
                map.put("col_2", "col_2_item_" + i);
                map.put("col_3", "col_3_item_" + i);
                fillMaps.add(map);
            }

            // my datasource is now changed, I want my adapter to know this and// update my ListView
            adapter.notifyDataSetChanged();
        }
    }
}

Solution 2:

As i remember and as a logical conclusion: In the moment of your call the reference of this is null. this will be accessed in the constructor of your ListView thus a NullPointerException will be raised. If you want to create it dynamically you have to call the ListView constructor within the onCreate method of your Activity.

At least it's better to implement it as suggested by MartijnVanMierloo, if possible.

Post a Comment for "Android - Having Trouble Updating A Listadapter After An Asynch Call"