gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:01:01 25 4
gpt4 key购买 nike

我有一个多选门票列表。所以我可以选择多张票并一次删除它们。但某些特定票证无法从列表中删除。所以我不想让它们在单击该项目时选择/选中多个选择。因此,我尝试使用 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)

这就是我所做的一切,但它对我不起作用。怎么解决呢???

最佳答案

我找到了解决方案,以防有人需要知道,问题再次回调 onItemCheckedStateChanged() 方法,因为

listView.setItemChecked(position, false);

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

if (listView.isItemChecked(position))
listView.setItemChecked(position, false);

它必须像魅力一样发挥作用。

关于java - 如何在多选 ListView 中以编程方式取消选择/取消选中所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com