删除 Android 中的 ListView 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2558591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:18:24  来源:igfitidea点击:

Remove ListView items in Android

androidandroid-listview

提问by kavitha

Can somebody please give me an example code of removing all ListView items and replacing with new items?

有人可以给我一个删除所有 ListView 项目并替换为新项目的示例代码吗?

I tried replacing the adapter items without success. My code is

我尝试更换适配器项目但没有成功。我的代码是

populateList(){

 results //populated arraylist with strings

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results);

 listview.setAdapter(adapter);
 adapter.notifyDataSetChanged();
 listview.setOnItemClickListener(this);

}

// now populating list again

repopulateList(){

 results1 //populated arraylist with strings

 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results1);

 listview.setAdapter(adapter1);
 adapter1.notifyDataSetChanged();
 listview.setOnItemClickListener(this);
}

Here repopulateList()method will add to ListView items, but it doesn't remove/replace all ListView items.

HererepopulateList()方法将添加到 ListView 项目,但它不会删除/替换所有 ListView 项目。

回答by esharp

You will want to remove()the item from your adapter object and then just run the notifyDatasetChanged()on the Adapter, any ListViews will (should) recycle and update on it's own.

您将需要remove()适配器对象中的项目,然后notifyDatasetChanged()在适配器上运行,任何ListViews 都(应该)自行回收和更新。

Here's a brief activity example with AlertDialogs:

这是一个带有AlertDialogs的简短活动示例:

adapter = new MyListAdapter(this);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
        adb.setTitle("Delete?");
        adb.setMessage("Are you sure you want to delete " + position);
        final int positionToRemove = position;
        adb.setNegativeButton("Cancel", null);
        adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MyDataObject.remove(positionToRemove);
                adapter.notifyDataSetChanged();
            }});
        adb.show();
        }
    });

回答by Labeeb Panampullan

I think if u add the following code, it will work

我想如果你添加以下代码,它会起作用

listview.invalidateViews();

To remove an item, Just remove that item from the arraylist that we passed to the adapter and do listview.invalidateViews();
This will refresh the listview

要删除一个项目,只需从我们传递给适配器的 arraylist 中删除该项目,然后执行listview.invalidateViews();
这将刷新列表视图

回答by Janusz

You can use

您可以使用

adapter.clear() 

that will remove all item of your first adapter then you could either set another adapter or reuse the adapter and add the items to the old adapter. If you use

这将删除您的第一个适配器的所有项目,然后您可以设置另一个适配器或重新使用该适配器并将这些项目添加到旧适配器。如果你使用

adapter.add()

to add data to your list you don't need to call notifyDataSetChanged

要将数据添加到您的列表中,您无需调用 notifyDataSetChanged

回答by Rex

int count = adapter.getCount();
for (int i = 0; i < count; i++) {
 adapter.remove(adapter.getItem(i));
}

then call notifyDataSetChanged();

然后打电话 notifyDataSetChanged();

回答by Atma

Remove it from the adapter and then notify the arrayadapter that data set has changed.

将其从适配器中移除,然后通知阵列适配器数据集已更改。

m_adapter.remove(o);
m_adapter.notifyDataSetChanged();

回答by HatemTmi

  • You should use only one adapter binded with the list of data you need to list
  • When you need to remove and replace all items, you have to clear all items from data-list using "list.clear()" and then add new data using "list.addAll(List)"
  • 您应该只使用一个与您需要列出的数据列表绑定的适配器
  • 当您需要删除和替换所有项目时,您必须使用“list.clear()”从数据列表中清除所有项目,然后使用“list.addAll(List)”添加新数据

here an example:

这里有一个例子:

List<String> myList = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,       android.R.layout.simple_list_item_1, myList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);

populateList(){
   List<String> result = getDataMethods();

   myList.addAll(result);
   adapter.notifyDataSetChanged();
}

repopulateList(){
   List<String> result = getDataMethods();

   myList.clear();
   myList.addAll(result);
   adapter.notifyDataSetChanged();
}

回答by user590849

            names = db.getSites();
            la = new ArrayAdapter<String>(EditSiteList.this,
                    android.R.layout.simple_list_item_1, names);
            EditSiteList.this.setListAdapter(la);
            listview.invalidateViews();

this code works fine for me.

这段代码对我来说很好用。

回答by Nadhir Titaouine

Try this code, it works for me.

试试这个代码,它对我有用。

public class Third extends ListActivity {
private ArrayAdapter<String> adapter;
private List<String> liste;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);
     String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
     liste = new ArrayList<String>();
     Collections.addAll(liste, values);
     adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, liste);
     setListAdapter(adapter);
}
 @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
     liste.remove(position);
     adapter.notifyDataSetChanged();
  }
}

回答by Muzaffer Onur DA?HAN

At first you should remove the item from your list. Later you may empty your adapter and refill it with new list.

首先,您应该从列表中删除该项目。稍后您可以清空适配器并用新列表重新填充它。

private void add(final List<Track> trackList) {
    MyAdapter bindingData = new MyAdapter(MyActivity.this, trackList);

    list = (ListView) findViewById(R.id.my_list); // TODO

    list.setAdapter(bindingData);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                final int position, long id) {
            // ShowPlacePref(places, position);
            AlertDialog.Builder showPlace = new AlertDialog.Builder(
                    Favoriler.this);
            showPlace.setMessage("Remove from list?");
            showPlace.setPositiveButton("DELETE", new OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {    

                    trackList.remove(position); //FIRST OF ALL REMOVE ITEM FROM LIST
                    list.setAdapter(null); // THEN EMPTY YOUR ADAPTER

                    add(trackList); // AT LAST REFILL YOUR LISTVIEW (Recursively)

                }

            });
            showPlace.setNegativeButton("CANCEL", new OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            showPlace.show();
        }

    });

}

回答by zheek

It's simple .First you should clear your collection and after clear list like this code :

这很简单。首先你应该清除你的收藏,然后像下面的代码一样清除列表:

 yourCollection.clear();
 setListAdapter(null);