• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to remove listview all items

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 remove the listview items dynamically.

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

  • 36
Reply Report
      • 1
    • Calling setListAdapter() with a new Adapter object would be expensive. Just remove all the items in the adapter using clear() and add the new data to existing Adapter object using add()/addAll() and call notifyDatasetChanged().
    • Here if you click on the button old data listview item will be removed after that new items will be added to the list view. is it possible with this?

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

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

// refresh the View
  • 25
Reply Report

if you used List object and passed to the adapter you can remove the value from the List object and than call the notifyDataSetChanged() using adapter object.

for e.g.

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

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

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


for remove do this way

list.remove(index); //or

or without list object remove item from list.

  • 10
Reply Report

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

values = new ArrayList<String>();

ArrayAdapter <String> adapter;
adapter = new ArrayAdapter<String>(this, R.layout.list,android.R.id.text1, values); 
  • 3
Reply Report

An other approach after trying the solutions below. When you need it clear, just initialise your list to new clear new list.

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

Or set visibility to Gone / Invisible up to need

  • 0
Reply Report

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.add(new YourClassName(yourParameter1, yourParameter2, ...));
  • 0
Reply Report