Android - Custom ListView Adapter - Multi Selection remove - Indexoutofbounds - why?

前端 未结 6 556
终归单人心
终归单人心 2021-01-18 11:01

I have a custom Listview using a adapter class to extend ArrayAdapter of a Item class. I have the ability to change between choice modes of NONE,Single and Multi. This all

6条回答
  •  猫巷女王i
    2021-01-18 11:20

    This is a documented bug, please vote for it:

    https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=64596

    My use case is the ListView configured as ListView.CHOICE_MODE_SINGLE, I tried @Peter Tran's suggestion without any luck. Here is the workaround that is successful for me:

        myAdapter.deleteRow(listView.getCheckedItemPosition());
        int checkedIndex = listView.getCheckedItemPosition();
        System.out.println("checkedIndex="+checkedIndex);
    
        int count=myAdapter.getCount();
        if (checkedIndex==count) listView.setItemChecked(count-1, true);
    

    My test is to manually select the last item on the list(count-1). I omitted the handling for count==0 case but that will most likely be needed. I observed that println() always prints the index of the deleted row. myAdapter.deleteRow() notifies of data change to listeners which says to me ListView isn't properly updating it's checked indices. I've tried this code with hasStableIds() returning both true and false from the custom adapter with the same results.

提交回复
热议问题