开发者

Delete selected item from List view

开发者 https://www.devze.com 2023-03-07 23:43 出处:网络
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 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...


Try adding this after removing the item.

adapter.notifyDataSetChanged();


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号