How to remove listview all items

后端 未结 11 2130
野的像风
野的像风 2020-12-15 15:36

In my app, if you click on a button then i want to remove all the listview items. Here, i am using the base adapter for adding the items to the list view.

How can i

相关标签:
11条回答
  • 2020-12-15 16:15

    Call setListAdapter() again. This time with an empty ArrayList.

    0 讨论(0)
  • 2020-12-15 16:15

    ListView operates based on the underlying data in the Adapter. In order to clear the ListView you need to do two things:

    1. Clear the data that you set from adapter.
    2. Refresh the view by calling notifyDataSetChanged

    For example, see the skeleton of SampleAdapter below that extends the 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;
        }
    }
    

    Here you have the ArrayList<String> data as the data for your Adapter. While you might not necessary use ArrayList, you will have something similar in your code to represent the data in your ListView

    Next you provide a method to clear this data, the implementation of this method is to clear the underlying data structure

    
    public void clearData() {
        // clear the data
        data.clear();
    }
    

    If you are using any subclass of Collection, they will have clear() method that you could use as above.

    Once you have this method, you want to call clearData and notifyDataSetChanged on your onClick thus the code for onClick will look something like:

    
    // listView is your instance of your ListView
    SampleAdapter sampleAdapter = (SampleAdapter)listView.getAdapter();
    sampleAdapter.clearData();
    
    // refresh the View
    sampleAdapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-12-15 16:20

    I just clean the arraylist , try values.clear();

    values = new ArrayList<String>();
    values.clear();
    
    ArrayAdapter <String> adapter;
    adapter = new ArrayAdapter<String>(this, R.layout.list,android.R.id.text1, values); 
    lista.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-12-15 16:21

    Remove the data from the adapter and call adapter.notifyDataSetChanged();

    0 讨论(0)
  • 2020-12-15 16:24

    For me worked this way:

    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);
    
    0 讨论(0)
  • 2020-12-15 16:25

    You can do this:

    listView.setAdapter(null);
    
    0 讨论(0)
提交回复
热议问题