Reputation: 29
I am having a multi-select list of tickets. So I can select multiple tickets and delete them at a time. But some particular tickets cant be deleted from the list. So I dont want to make them selected/checked in multiple select on click on that item. So I tried to deselect the particular tickets when clicking on it by using listView.setItemChecked(position, false)
but its not working.
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
This method is being called on click of the items:
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
System.out.println("############### Position : " +position +" id: "+id+ " checked : "+checked );
//Getting the ticket
ticket = ticketList.get(position);
if(ticket.getparticularIds()!= null && !ticket.getparticularIds().isEmpty()) {
//***Do not select this ticket***
Toast.makeText(mContext, "Not Allowed", Toast.LENGTH_SHORT).show();
// De-selecting the particular ticket
listView.setItemChecked(position, false);
mAdapter.notifyDataSetChanged();
// Set the CAB title according to total checked items
mode.setTitle(listView.getCheckedItemCount() + " Selected ");
}
else {
mAdapter.toggleSelection(position);
}
mode.setTitle(listView.getCheckedItemCount() + " Selected ");
}
After Clicking on particular ticket getting this output:
01-16 01:48:27.254 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.254 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.284 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.284 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.304 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.304 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.324 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.334 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.334 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.364 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.374 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.374 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.394 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.414 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.424 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.444 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.454 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.474 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.484 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.494 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.514 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.524 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.534 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.544 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.554 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.564 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.574 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.574 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.584 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.584 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.594 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.614 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.614 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.624 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.634 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.644 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.654 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.674 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.684 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.694 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.704 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.704 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.714 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.724 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.734 1426-1426/com.demo.lv.android I/System.out﹕ ############### Position : 1 id: 1 checked : false
01-16 01:48:27.734 1426-1426/com.demo.lv.android I/dalvikvm﹕ threadid=1: stack overflow on call to Landroid/util/ContainerHelpers;.binarySearch:ILII
01-16 01:48:27.744 1426-1426/com.demo.lv.android I/dalvikvm﹕ method requires 32+20+0=52 bytes, fp is 0xb0282330 (48 left)
01-16 01:48:27.744 1426-1426/com.demo.lv.android I/dalvikvm﹕ expanding stack end (0xb0282300 to 0xb0282000)
01-16 01:48:27.744 1426-1426/com.demo.lv.android I/dalvikvm﹕ Shrank stack (to 0xb0282300, curFrame is 0xb0282604)
01-16 01:48:27.744 1426-1426/com.demo.lv.android D/AndroidRuntime﹕ Shutting down VM
01-16 01:48:27.744 1426-1426/com.demo.lv.android W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a6eba8)
01-16 01:48:27.894 1426-1426/com.demo.lv.android D/dalvikvm﹕ GC_FOR_ALLOC freed 1635K, 9% free 20465K/22260K, paused 40ms, total 41ms
01-16 01:48:28.014 1426-1426/com.demo.lv.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.demo.lv.android, PID: 1426
android.view.InflateException: Binary XML file line #27: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:621)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.widget.Toast.makeText(Toast.java:245)
at com.demo.lv.android.activities.TicketListFragment$2.onItemCheckedStateChanged(TicketListFragment.java:231)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6364)
at android.widget.AbsListView.setItemChecked(AbsListView.java:1026)
at com.demo.lv.android.activities.TicketListFragment$2.onItemCheckedStateChanged(TicketListFragment.java:233)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6364)
at android.widget.AbsListView.setItemChecked(AbsListView.java:1026)
at com.demo.lv.android.activities.TicketListFragment$2.onItemCheckedStateChanged(TicketListFragment.java:233)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6364)
at android.widget.AbsListView.setItemChecked(AbsListView.java:1026)
at com.demo.lv.android.activities.TicketListFragment$2.onItemCheckedStateChanged(TicketListFragment.java:233)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6364)
at android.widget.AbsListView.setItemChecked(AbsListView.java:1026)
at com.demo.lv.android.activities.TicketListFragment$2.onItemCheckedStateChanged(TicketListFragment.java:233)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:6364)
at android.widget.AbsListView.setItemChecked(AbsListView.java:1026)
This is all I have done but its not working for me. How can I solve it???
Upvotes: 0
Views: 1361
Reputation: 4044
I found the solution incase any one need to know, problem is again calling back onItemCheckedStateChanged() method because of
listView.setItemChecked(position, false);
it will set that position selected item to false then it will call again onItemCheckedStateChanged() and this time value is false. so it will shows stack overflow error or inside infinite loop calling same function. so need to replace with this code.
if (listView.isItemChecked(position))
listView.setItemChecked(position, false);
It must work like charm.
Upvotes: 1