Removing muliple items from listview using Check b

2020-01-29 08:10发布

Hi i am stuck with deleting multiple items from listview using checkbox. I am able to select multiple items from listview but only the first item that is selected, is getting deleted while other items are not being deleted. Here is my code. Thanks in Advance!

public class MainActivity extends Activity 
{
    ListView lv;
    ArrayAdapter<String> adapter;
    Button delete;
    ArrayList<String> data = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.listView1);
        delete = (Button)findViewById(R.id.button1);
        data.add("Windows");
        data.add("Android");
        data.add("Apple");
        data.add("Blackberry");
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, data);
        lv.setAdapter(adapter);

        delete.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                deleteCheckedItems();
                adapter.notifyDataSetChanged();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    private void deleteCheckedItems() {
        int count = adapter.getCount();

        for (int i = 0; i < count; i++) 
        {
            if (lv.isItemChecked(i))
            {
               data.remove(i);
            }
        }
    }

}

4条回答
狗以群分
2楼-- · 2020-01-29 08:41

Use removeAll for data and pass an array of elements to be deleted to the method. The reson is this:

for example you have 10 elements, after the 4th element deleted, all indexes 5-9 are moved to 4-8, thus your cycle is not correct because you use old indexes.

查看更多
Root(大扎)
3楼-- · 2020-01-29 08:45

Maybe you should try code below. The code is based on recurrency. After the "OnClick" event the Bremselected is called. The ListView is iterated until the "checked" checkbox is founded. Then item is removed and another time (there is recurrency) the Bremselected is called and the new iteration begins from scratch with the "size" decremented.

public void Bremselected(View v)
{
final int size=lv.getChildCount(); /// lv=(ListView) 

    for(int i=0;i<size;i++){
    CheckBox mchb=(MCheckBox) lv.getChildAt(i);

        if(mchb.isChecked()){
            lv.removeViewAt(i);

            Bremselected(v);
            break;
        }

    }

}

查看更多
何必那么认真
4楼-- · 2020-01-29 08:46

Use a SparseBooleanArray to get the checked items and then delete the same and refresh listview.

public class MainActivity extends Activity 
{
    ListView lv;
    ArrayAdapter<String> adapter;
    Button delete;
    ArrayList<String> data = new ArrayList<String>();
    SparseBooleanArray mCheckStates ;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        lv = (ListView)findViewById(R.id.listView1);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        delete = (Button)findViewById(R.id.button1);
        data.add("Windows");
        data.add("Android");
        data.add("Apple");
        data.add("Blackberry");
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, data);
        lv.setAdapter(adapter);

        delete.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                SparseBooleanArray checked = lv.getCheckedItemPositions();
                for (int i = 0; i < lv.getCount(); i++){

                    if (checked.get(i)==true)
                    {
                         data.remove(i);

                    } 
                    adapter.notifyDataSetChanged(); 

                }
                 lv.clearChoices();               
            }
        });


    }
}

Edit:

Try the below

 delete.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

             SparseBooleanArray checkedItemPositions = lv.getCheckedItemPositions();
             int itemCount = lv.getCount();

             for(int i=itemCount-1; i >= 0; i--){
                 if(checkedItemPositions.get(i)){
                     adapter.remove(data.get(i));
                 }
             }
             checkedItemPositions.clear();
             adapter.notifyDataSetChanged();

        }
    });
查看更多
够拽才男人
5楼-- · 2020-01-29 09:00
delete.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {

       SparseBooleanArray checkedItemPositions = lv.getCheckedItemPositions();
       int itemCount = lv.getCount();

       for(int i=0; i <itemCount; i++){
           if(checkedItemPositions.get(i)){
               adapter.remove(data.get(i)); 
               --i;
           }
       }
       checkedItemPositions.clear();
       adapter.notifyDataSetChanged();

    }
});
查看更多
登录 后发表回答