I have a listView in an android app which works fine when I first populate 
it - it displays and scrolls with no problem. But if I load in a new, 
smaller dataset and call notifyDataSetChanged() the app crashes because 
getView() gets called with a position value that's bigger than the dataset, 
i.e., if listItems.size==6, valid values for position should be [0]-[5] but 
getView() is called with 6, so I'm getting index out of bounds when I try 
to access an item in my list. While investigating this I noticed that* 
ListView's getCount() is still returning the old value of 12.*

Details:

...In MyListActivity, which is a ListActivity . . .

public static ListView lv;   // my ListView in the code

... during onCreate() . . .

   setContentView(R.layout.mylist);
   lv = getListView();

create the adapter and bind it . . .

mylistadapter = new MyListAdapter(MyListActivity.this);
setListAdapter(mylistadapter);   // bind the adapter

...the data source is an ArrayList called listItems. The first time around 
it has 12 items in it; later I clear it and add in 6 items. (see below)

public static ArrayList<String>listItems=new ArrayList<String>();

... in my adapter, which is a BaseAdapter, my override of getCount() looks 
like this. After shrinking listItems to 6 it correctly returns 6.

@Override
public int getCount() {
    return listItems.size();
}

... To shrink my dataset to 6 I first do a

listItems.clear();
lv.invalidate();   // my (failed) attempt to get lv to reset its count

... and then add in the 6 new items. After adding each item to listItems I 
do a notifyDataSetChanged() on the adapter. What I've noticed is that if* I 
do an lv.getCount it returns 12, i.e., the value it was before doing the 
listItems.clear()* and invalidate(). Why does the ListView still think it's 
12? How do I reset it? Is that why the adapter thinks it's too big?


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to