Honestly, I am glad I got this to work the first time. Otherwise, I totally would have been stuck for who knows how long.

It probably had been more than 2 years since I used ListView and modified its contents like adding, removing and updating. When I made another go recently, I was confused why the ListView does not refresh its content.

I thought calling the notifyDataSetChanged() of ArrayAdapter after add() or remove() will do the trick in the onPostExecute() of the AsyncTask class.

The solution is not to use the add() or remove() method of the ArrayAdapter class. Instead, you place a global variable in your class for the ArrayList object that you will pass to the ArrayAdapter.

I instantiate an ArrayAdapter like this:

Make sure the myArrayList variable is global. So that within your AsyncTask class, you can call add() or remove() method of the ArrayAdapter class within doInBackground() of AsyncTask and call adapter.notifyDataSetChanged() onPostExecute().

That should refresh your ListView contents.

See sample below:

This one is tricky. The main culprit actually lies in your ListView attribute android:layout_height. You probably set it to wrap_content. While the result may sometimes be harmless e.g. setting values to TextView and ImageView, try adding some threads within the getView() method and you will be surprised why it gets called many times.

This is because if you set the ListView height to wrap_content, every View in the list has to rescaled, so the method keeps getting called. Replace the height with a fill_parent to solve this issue.

A value of 0dip works as well provided you set a layout_weight value.

Moral lesson? Never use wrap_content as height value of a ListView widget.

Related Posts Plugin for WordPress, Blogger...