0

I am using this code in order to do this, but it is not functional at this point. I can select the checkbox, and when I click the button, nothing happens.

SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
   ...

    cb = (CheckBox)findViewById(R.id.checkBox1);    
    adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
            from, to);
    listthings.setAdapter(adapter);
    listthings.setOnItemClickListener(this);

    deleteButton = (Button) findViewById(R.id.deleteButton);
    deleteButton.setOnClickListener(this);

 ...

public void onClick(View v) {

    if (v == this.deleteButton) {
//      Toast.makeText(this, "delete clicked", Toast.LENGTH_SHORT).show();
        deleteCheckedItems();
    }
}

...
 private void deleteCheckedItems() {
    int count = this.listthings.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.listthings.isItemChecked(i)) {
            painItems.remove(i);
            adapter.notifyDataSetChanged();
                            listthings.invalidateViews();
        }
    }

Any help appreciated. Thank You in advance.

Kgrover
  • 2,106
  • 2
  • 36
  • 54

1 Answers1

1

1) do not put adapter.notifyDataSetChanged(); into your loop but after deleteCheckedItems();

2) Why did you use: listthings.invalidateViews(); ? You can remove this line, this will redraw your list

3) why havn't you accepted my answer here: How to implement check box in listView Android to delete listItems

4) have you tested a toast after painItems.remove(i); that will show you if items are really removed? Or maybe just after calling deleteCheckedItems(), display the new painItems to see if everything worked

Community
  • 1
  • 1
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 1) Eclipse is not allowing me to simply put this line outside of the method. 2)Done. 3)I'm Sorry. I have not done this because my code is still not deleting the item. 4)If I put a toast after painitems.remove(i), it doesn't show. What could be the reason for this? – Kgrover Jul 26 '11 at 20:57
  • The 4) issue: 2 reasons: count == 0 or isCheckedItem is always null... Can you test? The 1) issue: maybe you can pass the adapter (and the listView) as parameter: deleteCheckedItems(SimpleAdapter adapter, Listview myListView), etc.. – Waza_Be Jul 27 '11 at 04:26
  • No testing needed. I just used LongClickListener instead of CheckBoxes... :P Thank you very much for your help. – Kgrover Jul 27 '11 at 04:36