androidlistviewsimpleadapter

How to clear elements from SimpleAdapter?


I am trying to fill the listview on a SET button.As i select the values spinner and according to list will fill. Problem i am facing is at many times i click on SET button it will add items to listview . enter image description here

   setButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
       for (int i = 0; i < DeviceID.size(); i++) 
       {

                //initialize row data
                for (int j = 0; j < 5; j++) {
                    if (j == 0)
                        str = DATE.get(i);
                    else if (j == 1)
                        str = TIME.get(i);
                    else if (j == 2)
                        str = DeviceID.get(i);
                    else if (j == 3)
                        str = SMSTEXT.get(i);
                    map.put(columnTags[j], str);
                }
                mylistData.add(map);
            }

   final String[] columnTags = new String[]{"one", "two", "three", "four", "five", "six", "lat", "log"};
    final int[] columnIds = new int[]{R.id.textView5, R.id.textView8, R.id.textView9,R.id.checkbox, R.id.textView10};
    arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.location_locator_textview, columnTags, columnIds);

     listview.setAdapter(arrayAdapter);
     arrayAdapter.notifyDataSetChanged();
     }
    });

Solution

  • It looks like your list mylistData is a global variable. So, the old items are still in the list.

    Either make it local inside the onClick method, or clear it in the first line of the onClick method. (Also the same case for map)

    public void onClick(View view){
        mylistData.clear();
        map.clear();
        //your code
    }