如何在多选ListView中以编程方式取消选择/取消选中所选项

时间:2015-01-16 10:36:12

标签: java android listview

我有一个多选票名单。所以我可以选择多张票并一次删除它们。但是某些特定的票不能从列表中删除。因此,我不希望在点击该项目时选择/选中多个选择。所以我尝试使用listView.setItemChecked(position, false)点击它时取消选择特定的票证,但它不起作用。

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

点击以下项目时会调用此方法:

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 ");
        }

点击获得此输出的特定故障单后:

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)

这就是我所做的一切,但它不适合我。我怎么解决呢???

1 个答案:

答案 0 :(得分:1)

我发现任何人都需要知道解决方案,问题是再次回调onItemCheckedStateChanged()方法因为

controllerAs

它会将所选位置设置为false,然后再次调用onItemCheckedStateChanged(),此时间值为false。所以它会显示堆栈溢出错误或内部无限循环调用相同的函数。 所以需要替换这段代码。

  <integer-array name="accordion_visibility">
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>0</item>
</integer-array>

它必须像魅力一样工作。