Android 如何删除listview所有项目

Android 如何删除listview所有项目,android,listview,adapter,Android,Listview,Adapter,在我的应用程序中,如果单击某个按钮,则我希望删除所有listview项目。 在这里,我使用基本适配器将项目添加到列表视图中 如何动态删除listview项 从适配器中删除数据并调用适配器.notifyDataSetChanged()再次调用setListAdapter()。这次使用空的ArrayList。如果使用List对象并传递给适配器,则可以从List对象中删除该值,然后使用adapter对象调用notifyDataSetChanged() 例如 List<String> lis

在我的应用程序中,如果单击某个按钮,则我希望删除所有listview项目。 在这里,我使用基本适配器将项目添加到列表视图中


如何动态删除listview项

适配器中删除数据
并调用
适配器.notifyDataSetChanged()

再次调用
setListAdapter()
。这次使用空的ArrayList。

如果使用List对象并传递给适配器,则可以从List对象中删除该值,然后使用adapter对象调用notifyDataSetChanged()

例如

List<String> list = new ArrayList<String>();
ArrayAdapter adapter;


adapter = new ArrayAdapter<String>(DeleteManyTask.this, 
            android.R.layout.simple_list_item_1,
            (String[])list.toArray(new String[0]));

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

listview.setAdapter(listAdapter);
或不使用列表对象从列表中删除项目

adapter.clear();
adpater.notifyDataSetChanged();

使用符合您要求的以下任一选项

listview.removeViews(1,listview.getChildCount());


ListView
基于
适配器中的底层数据进行操作。要清除
列表视图
,您需要做两件事:

  • 清除从适配器设置的数据
  • 通过调用
    notifyDataSetChanged
  • 例如,请参见下面扩展
    BaseAdapter

    
    public class SampleAdapter extends BaseAdapter {
    
        ArrayList<String> data;
    
        public SampleAdapter() {
            this.data = new ArrayList<String>();
        }
    
        public int getCount() {
            return data.size();
        }
    
        public Object getItem(int position) {
            return data.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            // your View
            return null;
        }
    }
    
    如果您使用的是的任何子类,它们将具有您可以如上所述使用的方法

    使用此方法后,您希望在
    onClick
    上调用
    clearData
    notifyDataSetChanged
    ,因此
    onClick
    的代码如下所示:

    
    // listView is your instance of your ListView
    SampleAdapter sampleAdapter = (SampleAdapter)listView.getAdapter();
    sampleAdapter.clearData();
    
    // refresh the View
    sampleAdapter.notifyDataSetChanged();
    

    我使用了这句话,它对我起了作用:

       setListAdapter(null)
    

    这个函数调用一个默认构造函数,该构造函数在类extends BaseAdapter中不做任何操作。

    我只是清理arraylist,然后尝试values.clear()

    values=newarraylist();
    value.clear();
    阵列适配器;
    adapter=newarrayadapter(这个,R.layout.list,android.R.id.text1,值);
    setAdapter(适配器);
    
    您只能使用

     lv.setAdapter(null);
    

    在尝试以下解决方案之后,还有另一种方法。当您需要清除时,只需将您的列表初始化为“新建”“清除”“新建列表”

    List<ModelData> dataLists = new ArrayList<>();
                    RaporAdapter adapter = new RaporAdapter(AyrintiliRapor.this, dataLists);
                    listview.setAdapter(adapter);
    

    对我来说,这是一种工作方式:

    private ListView yourListViewName;
    private List<YourClassName> yourListName;
    
      ...
    
    yourListName = new ArrayList<>();
    yourAdapterName = new yourAdapterName(this, R.layout.your_layout_name, yourListName);
    
      ...
    
    if (yourAdapterName.getCount() > 0) {
       yourAdapterName.clear();
       yourAdapterName.notifyDataSetChanged();
    }
    
    yourAdapterName.add(new YourClassName(yourParameter1, yourParameter2, ...));
    yourListViewName.setAdapter(yourAdapterName);
    
    private ListView yourListViewName;
    私人名单你的名字;
    ...
    yourListName=newarraylist();
    yourAdapterName=新建yourAdapterName(this,R.layout.your\u layout\u name,yourListName);
    ...
    如果(yourAdapterName.getCount()>0){
    你的适配器名称。清除();
    yourAdapterName.notifyDataSetChanged();
    }
    添加(新的YourClassName(yourParameter1,yourParameter2,…);
    yourListViewName.setAdapter(yourladaptername);
    
    您可以执行以下操作:

    listView.setAdapter(null);
    

    动态传递getcount,以清除传递零作为getcountlistview.setAdapter(null);谢谢你,阿比,这真的很有帮助。覆盖getCount方法…如果单击按钮,旧的数据listview项将在新项添加到列表视图后被删除。可以这样做吗?可以,您可以根据需要使用新数据调用setListAdapter()。使用新适配器对象调用setListAdapter()会很昂贵。只需使用clear()删除适配器中的所有项,并使用add()/addAll()将新数据添加到现有适配器对象中,然后调用notifyDatasetChanged()。我正在使用基本适配器。所以我没有明确的()方法。如何删除它?如何从适配器中删除数据?在声明适配器时,您将以某种形式传递值…从那里删除值…这就是问题所在。如何从适配器中删除项目。这将破坏android 2.1
     lv.setAdapter(null);
    
    List<ModelData> dataLists = new ArrayList<>();
                    RaporAdapter adapter = new RaporAdapter(AyrintiliRapor.this, dataLists);
                    listview.setAdapter(adapter);
    
    img_pdf.setVisibility(View.INVISIBLE);
    
    private ListView yourListViewName;
    private List<YourClassName> yourListName;
    
      ...
    
    yourListName = new ArrayList<>();
    yourAdapterName = new yourAdapterName(this, R.layout.your_layout_name, yourListName);
    
      ...
    
    if (yourAdapterName.getCount() > 0) {
       yourAdapterName.clear();
       yourAdapterName.notifyDataSetChanged();
    }
    
    yourAdapterName.add(new YourClassName(yourParameter1, yourParameter2, ...));
    yourListViewName.setAdapter(yourAdapterName);
    
    listView.setAdapter(null);