I have a problem with listView, I used ArrayList to store data, and a customized Adapter. But, when I remove all the data, and add one item again, it does not display anything in this list. What happens to my List, can anyone help me?
static ArrayList<String> chattingListData=new ArrayList<String>();
static IconicAdapter chattingListDataAdapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
chattingListDataAdapter=new IconicAdapter(this);
setListAdapter(chattingListDataAdapter);
registerForContextMenu(this.getListView());
}
class IconicAdapter extends ArrayAdapter {
Ac开发者_如何学Gotivity context;
IconicAdapter(Activity context) {
super(context, R.layout.chatting_list, chattingListData);
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.chatting_list, null);
TextView label=(TextView)row.findViewById(R.id.chattinglist_userName);
label.setText(chattingListData.get(position));
return(row);
}
}
I use static ArrayList to modify data from outside, when I start the ListView activity and add data, it's Ok, but when I remove all data, I can not add anymore to the data list. Please help me.
I use static ArrayList to modify data from outside
Don't do that.
Step #1: Use a non-static ArrayList
data member as the basis for your ArrayAdapter
.
Step #2: Expose methods on your activity that adds and removes items via the add()
, insert()
, and remove()
methods on ArrayAdapter
. By using these methods, your ListView
will automatically update when you make the changes.
Step #3: Use the methods you wrote in Step #2 to modify the contents of your ListView
, by whoever is doing this (not shown in your code).
You can also use the Adapter's notifyDataSetChanged() method, in order to force it to refresh.
精彩评论