100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android listview删除刷新 如何刷新Android ListView?

android listview删除刷新 如何刷新Android ListView?

时间:2021-11-25 06:01:22

相关推荐

android listview删除刷新 如何刷新Android ListView?

添加/删除动态数据后如何刷新Android ListView ?

#1楼

请忽略所有对此问题的invalidate() , invalidateViews() , requestLayout() ,...答案。

故障排除

如果调用notifyDataSetChanged()不起作用,则所有布局方法也无济于事。 相信我ListView已正确更新。 如果找不到差异,则需要检查适配器中数据的来源。

如果这只是一个集合,则您要保留在内存中,然后在调用notifyDataSetChanged()之前检查是否已从集合中实际删除或添加了项目。

如果您使用的是数据库或服务后端,则必须在调用notifyDataSetChanged()之前调用该方法以再次检索信息(或操作内存中的数据notifyDataSetChanged() 。

事情是, notifyDataSetChanged仅在数据集已更改的情况下有效。 因此,如果找不到更改,那就可以在这里查找。 如果需要调试。

ArrayAdapter与BaseAdapter

我确实发现,使用适配器可以管理集合,就像使用BaseAdapter更好。 某些适配器(例如ArrayAdapter)已经管理了自己的集合,这使得更难到达正确的集合进行更新。 在大多数情况下,这实际上只是不必要的额外难度。

UI线程

确实,这必须从UI线程中调用。 其他答案包含有关如何实现此目的的示例。 但是,仅当您正在UI线程外部处理此信息时才需要这样做。 那是来自服务或非UI线程的。 在简单的情况下,您将通过单击按钮或其他活动/片段来更新数据。 因此仍在UI线程内。 无需始终将该runOnUiTrhead弹出。

快速示例项目

可以在/hanscappelle/so-2250770.git中找到。 只需克隆并在Android Studio中打开项目(渐变)。 该项目的MainAcitivity使用所有随机数据构建一个ListView 。 可以使用操作菜单刷新此列表。

我为此示例ModelObject创建的适配器实现公开了数据收集

public class MyListAdapter extends BaseAdapter {

/**

* this is our own collection of data, can be anything we

* want it to be as long as we get the abstract methods

* implemented using this data and work on this data

* (see getter) you should be fine

*/

private List mData;

/**

* our ctor for this adapter, we'll accept all the things

* we need here

*

* @param mData

*/

public MyListAdapter(final Context context, final List mData) {

this.mData = mData;

this.mContext = context;

}

public List getData() {

return mData;

}

// implement all abstract methods here

}

来自MainActivity的代码

public class MainActivity extends Activity {

private MyListAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ListView list = (ListView) findViewById(R.id.list);

// create some dummy data here

List objects = getRandomData();

// and put it into an adapter for the list

mAdapter = new MyListAdapter(this, objects);

list.setAdapter(mAdapter);

// mAdapter is available in the helper methods below and the

// data will be updated based on action menu interactions

// you could also keep the reference to the android ListView

// object instead and use the {@link ListView#getAdapter()}

// method instead. However you would have to cast that adapter

// to your own instance every time

}

/**

* helper to show what happens when all data is new

*/

private void reloadAllData(){

// get new modified random data

List objects = getRandomData();

// update data in our adapter

mAdapter.getData().clear();

mAdapter.getData().addAll(objects);

// fire the event

mAdapter.notifyDataSetChanged();

}

/**

* helper to show how only changing properties of data

* elements also works

*/

private void scrambleChecked(){

Random random = new Random();

// update data in our adapter, iterate all objects and

// resetting the checked option

for( ModelObject mo : mAdapter.getData()) {

mo.setChecked(random.nextBoolean());

}

// fire the event

mAdapter.notifyDataSetChanged();

}

}

更多信息

关于listViews功能的另一篇不错的文章位于: http :///articles/AndroidListView/article.html

#2楼

如果要从服务更新UI列表视图,请在Main活动中将适配器设为静态,然后执行以下操作:

@Override

public void onDestroy() {

if (MainActivity.isInFront == true) {

if (MainActivity.adapter != null) {

MainActivity.adapter.notifyDataSetChanged();

}

MainActivity.listView.setAdapter(MainActivity.adapter);

}

}

#3楼

对我来说,更改sql数据库中的信息后,什么也无法刷新列表视图(是特定的可扩展列表视图),因此,如果notifyDataSetChanged()无法解决问题,您可以尝试先清除列表,然后在调用notifyDataSetChanged()之后再次添加列表。 例如

private List> arrayList;

List array1= getArrayList(...);

List array2= getArrayList(...);

arrayList.clear();

arrayList.add(array1);

arrayList.add(array2);

notifyDataSetChanged();

希望这对您有意义。

#4楼

我无法获取notifyDataSetChanged()来更新我的SimpleAdapter,因此我尝试首先使用removeAllViews()删除所有附加到父布局的视图,然后添加ListView,并且该视图可以正常工作,从而允许我更新用户界面:

LinearLayout results = (LinearLayout)findViewById(R.id.results);

ListView lv = new ListView(this);

ArrayList> list = new ArrayList>();

SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row,

new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );

for (...) {

HashMap map = new HashMap();

map.put("name", name);

map.put("dept", dept);

list.add(map);

}

lv.setAdapter(adapter);

results.removeAllViews();

results.addView(lv);

#5楼

使用SimpleCursorAdapter时可以在适配器上调用changeCursor(newCursor)。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。