android listView change dynamically
i have a list and corresponding adaptor. in list i have 20 items which is stored in an array. a event occurred and my list size decreased to 10 items(now in array 10 items are present). i call notifyDataSetChanged
to refresh the list. but i am getting null pointer exception. i think this is because this method
public View getView(int position, View convertView, ViewGroup parent)
is calling 开发者_如何学Go20 times and I have only 10 item so array[position] is returning null at position=11 in getView Method. am i thinking in right direction? i want to know more about getView but android documentation is not sufficient for me to understand the things.
You should fix
public int getCount ()
to return the correct count
u can use handler to change list view dynamically
private final Handler mHandler=new Handler()
{
@Override
public void handleMessage(Message msg) {
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings));
list.invalidate();
return;
}
};
if u want call handler use
handler.sendemptymessage(0);
you should reset the array list in adapter class (i.e at the time of calling notifyDatasetChanged() the array list should have that 10 items.
精彩评论