2

I have developed a code in which i have populated list view dynamically.

now i want to delete the selected item from list view on button click(on pressing delete button)

I have searched out this in this site but didn't got any exact solution so i am posting this question

please help me how to do this :

code on delete buttons onClickListener is as shown below :

DeleteButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            if (idx >= 0) {
                Log.v("Item index deleted", idx + "");
                idx = OdrLst.getCheckedItemPosition();
                String delete = (String) ((OdrLst.getAdapter())
                        .getItem(idx));
                // Long deteteId = OdrLst.getAdapter().getItemId(idx);
                Log.d("Item deleted", delete);
                Log.d("adapter count before", adapter.getCount() + "");
                Log.d("lv count before", OdrLst.getCount() + "");
                // Log.d("listitems count before", listItems.+"");
                adapter.remove(delete);
                //listItems.remove(idx);
                adapter.notifyDataSetChanged();
                OdrLst.setAdapter(adapter);
                // OdrLst.removeViewAt(idx);
                // adapter.clear();
                Log.d("adapter count after", adapter.getCount() + "");
                Log.d("lv count after", OdrLst.getCount() + "");
                //adapter.notifyDataSetChanged();
                // Log.v("adapter count after 1", adapter.getCount()+"");
            }
            // cleared = false; // <--- nope, we did not clear the value yet
            // delItem();
        }
    });

This code shows exact position and item to be deleted but the item not gets removed from the listview...

Shrey
  • 1,959
  • 2
  • 21
  • 44
  • Duplicate of [this question](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) – Kalarani May 23 '11 at 12:22
  • no its not the duplicate question as on this link i want to delete items from listview on click of delete button – Shrey May 23 '11 at 12:48
  • hello everybody i have edited the code of delete button as shown above now by this code all the items in the listview . can anybody please edit my code to delete only the selected item ? – Shrey May 30 '11 at 13:07

2 Answers2

2

Try adding this after removing the item.

adapter.notifyDataSetChanged();
Hades
  • 3,916
  • 3
  • 34
  • 74
  • do i need to setOnItemClickListener on my listview inside delete buttons OnClick event ??? – Shrey May 23 '11 at 12:51
  • 1
    Finally i am able to delete the selected item from listview . The code that worked for me is : public void onClick(View v) { if (idx != 0) { String delete = (String) ((OdrLst.getAdapter()).getItem(idx)); //Log.d("Item deleted", delete); adapter.remove(delete); adapter.notifyDataSetChanged(); OdrLst.setAdapter(adapter); //Log.d("adapter count after", adapter.getCount() + ""); //Log.d("lv count after", OdrLst.getCount() + ""); } } – Shrey May 31 '11 at 12:26
  • isn't that the same thing? :S It was clearly something to do with your adapter code. :) – Hades May 31 '11 at 12:35
1

You can make a customized Listview containing check boxes or imageview and then use Arraylist to get the items which were clicked in the list. refer these link: Remove item from the listview in Android

Community
  • 1
  • 1
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30