62

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

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

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);
}

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

4

12 回答 12

79

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

这是一个带有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();
        }
    });
于 2011-03-17T20:37:33.340 回答
24

我认为如果您添加以下代码,它将起作用

listview.invalidateViews();

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

于 2010-11-03T06:51:29.290 回答
19

您可以使用

adapter.clear() 

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

adapter.add()

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

于 2010-04-01T07:51:44.383 回答
15
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
 adapter.remove(adapter.getItem(i));
}

然后打电话notifyDataSetChanged();

于 2012-01-17T10:55:42.823 回答
10

从适配器中删除它,然后通知数组适配器数据集已更改。

m_adapter.remove(o);
m_adapter.notifyDataSetChanged();
于 2011-11-25T22:44:03.367 回答
4
  • 您应该只使用一个与您需要列出的数据列表绑定的适配器
  • 当您需要删除和替换所有项目时,您必须使用“list.clear()”从数据列表中清除所有项目,然后使用“list.addAll(List)”添加新数据

这里有一个例子:

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();
}
于 2013-08-28T14:54:20.433 回答
4
            names = db.getSites();
            la = new ArrayAdapter<String>(EditSiteList.this,
                    android.R.layout.simple_list_item_1, names);
            EditSiteList.this.setListAdapter(la);
            listview.invalidateViews();

这段代码对我来说很好。

于 2011-03-17T05:28:11.047 回答
3

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

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();
  }
}
于 2015-05-29T02:46:38.243 回答
1

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

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();
        }

    });

}
于 2015-01-25T14:21:27.647 回答
1

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

 yourCollection.clear();
 setListAdapter(null);
于 2015-03-06T11:57:44.923 回答
1

您还可以使用 listView.setOnItemLongClickListener 删除选定的项目。下面是代码。

// listView = 您的 ListView 的名称

  listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int 
        position, long id) {

        // it will get the position of selected item from the ListView

final int selected_item = position;

            new AlertDialog.Builder(MainActivity.this).
                    setIcon(android.R.drawable.ic_delete)
                    .setTitle("Are you sure...")
                    .setMessage("Do you want to delete the selected item..?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            list.remove(selected_item);
                            arrayAdapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("No" , null).show();

            return true;
        }
    });
于 2019-10-28T13:16:57.417 回答
0

//尝试这个:

String text = ((TextView) view).getText().toString();
适配器.删除(文本); 在此处输入图像描述

于 2021-09-14T15:43:15.373 回答