Calling notifyDataSetChanged on a ListAdapter

[亡魂溺海] 提交于 2020-02-02 06:27:26

问题


I have a ListAdapter as shown below:

setListAdapter(new ArrayAdapter<String>(DeleteMenu.this,             
                                  android.R.layout.simple_list_item_1, 
                                  classes));

I am trying to call notifyDataSetChanged() on it within an onListItemClick() function. I've tried a few different ways and looked at similar questions here on StackOverFlow but I still can't figure it out.

Can someone please help?

Edit: I should be more clear, I'm not getting an error, I simply don't know what to call the function notifyDataSetChanged() method on. Do I have to assign my ListAdapter to a variable and call it like var.notifyDataSetChanged()?


回答1:


You should call notifyDataSetChanged on Adapter.

Adapter adapter = new Adapter();
ListView list = (ListView) findViewById(R.id.listview);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();



回答2:


You can call the method notifyAdapterChanged() by getting a reference to the Adapter. You can either keep a local reference (like user1411084s answer), or retrieve it by calling:

getListAdapter()

Important to notice is that the interface ListAdapter doesn't provide the method notifyDataSetChanged(), so you should cast it to the type of the adapter you assigned earlier.

The result will look like this (able to call anywhere in your ListActivity/ListFragment:

((ArrayAdapter) getListAdapter()).notifyDataSetChanged();



回答3:


((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); 

See more info in this answer: https://stackoverflow.com/a/4198569/3994630



来源:https://stackoverflow.com/questions/13431745/calling-notifydatasetchanged-on-a-listadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!