【问题标题】:What is the correct way of refreshing a ListViews content?刷新 ListViews 内容的正确方法是什么?
【发布时间】:2013-08-16 15:32:27
【问题描述】:

我因此而陷入困境......因为每个问题(我认为)都是这样。

我根据它设置了 Activity 和 setAdapter 的 listview onCreate 方法,因此我无法刷新 LISTVIEW,因为 Activity 仅创建一次。

如果有人找到正确的答案,请指导我..请

【问题讨论】:

  • 到目前为止你尝试过什么?您实际面临什么问题?

标签: android android-listview refresh listadapter


【解决方案1】:

您可以在 ListView 的 Adapter 上调用 notifyDataSetChanged(),或在代码中的其他位置为您的 ListView 设置新的 Adapter

请查看此帖子以获取具体信息:

Android: how to refresh ListView contents?

摘自上面的帖子:

// this could be inside your oncreate method:
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(defaultAdapter);  // the defaultAdapter is the ListAdapter you first added to the listview, its a member-variable

// this will refresh the adapter and therefore refresh the listView
public void refreshListViewContent() {

    List<Item> newItems = getNewListViewItems(); // get the new listview items from somewhere

    defaultAdapter.clear();  // clear your adapter
    defaultAdapter.addAll(newItems); // add the new items
    defaultAdapter.notifyDataSetChanged();  // do the actual refresh
}

方法 refreshListViewContent() 可以从代码中的任何位置调用,例如当您单击按钮或类似的东西时:

Button button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {

           refreshListViewContent();
       }
 });

【讨论】:

  • 在设置适配器时我在哪里调用了 notifyDataSetChanged() 但它已经在创建时设置了我在我的第一个布局上设置列表视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2015-08-30
相关资源
最近更新 更多