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...

3
  • Duplicate of this question Commented May 23, 2011 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 Commented May 23, 2011 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 ? Commented May 30, 2011 at 13:07

2 Answers 2

2

Try adding this after removing the item.

adapter.notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

3 Comments

do i need to setOnItemClickListener on my listview inside delete buttons OnClick event ???
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() + ""); } }
isn't that the same thing? :S It was clearly something to do with your adapter code. :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.