安卓 - 自定义 ListView 适配器 - 多选删除 - IndexOutOfBoundsException - 为什么?

6
我有一个自定义的ListView,使用适配器类扩展Item类的ArrayAdapter。我有更改选择模式为无、单选和多选的能力。这一切都很好地运作。现在我正在尝试实现一种方法,可以在多选模式下从列表视图(和适配器)中删除项目。然而,当执行以下任何一项时,我会得到IndexOutOfBoundsException异常: 1)在SINGLE选择模式下删除最后一项(注意:任何前面的元素均可正常删除) 2)在多选选择模式下我再次不能移除最后一项 3)再次在多选模式下,我可以删除多个选择项中倒数第二个或更少的单个选择项,但2个或更多选择结果仍然导致索引越界错误。
我添加了调试日志来显示被删除的位置、getCheckItemPositions()的大小和我的循环计数器(i)以及被删除项的标题。如果我评论掉实际的listadpter.remove(position)行,那么日志输出似乎表明一切都工作正常。因此,我现在怀疑问题出现在我的适配器类getView方法中。但我的大脑已经精疲力尽了,我陷入了困境。
MainActivity.class - removeItems方法从button视图对象调用;
private void removeItems() {
    final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
    //final long[] checkedItemIds = listView.getCheckedItemIds();
    final int checkedItemsCount = checkedItems.size();

    Log.d("drp", "Adapter Count is: " + Integer.toString(mMyListViewAdapter.getCount()));
    if (checkedItems != null) {
        for (int i = checkedItemsCount-1; i >= 0 ; --i) {
            // This tells us the item position we are looking at
            // --
            final int position = checkedItems.keyAt(i);
            // This tells us the item status at the above position
            // --
            final boolean isChecked = checkedItems.valueAt(i);

            if (isChecked) {
                Item item = mMyListViewAdapter.getItem(position);
                Log.d("drp", "removing : " + Integer.toString(position) + " of " +Integer.toString(checkedItemsCount) + "-" + Integer.toString(i) + " - Title: " + mMyListViewAdapter.getItem(position).getTitle());
                mMyListViewAdapter.remove(item);

            }
        }
    }
}

适配器类;
public class MyListViewAdapter extends ArrayAdapter<Item>  implements OnItemClickListener{

private LayoutInflater mInflator;

/**
 * This is my view holder for getView method so don't need to call
 * findViewById all the time which results in speed increase
 */
static class ViewHolder {

    public TextView txtTitle;
    public TextView txtDescription;
    public TextView txtSessionCount;
    public ImageView listThumbnailImage;
    public ImageView listStatusIndicatorImage;
    public InertCheckBox Checkbox;
}

/**
 * Constructor from a list of items
 */
public MyListViewAdapter(Context context, List<Item> items) {
    super(context, 0, items);
    mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // This is how you would determine if this particular item is checked
    // when the view gets created
    // --
    final ListView lv = (ListView) parent;
    final boolean isChecked = lv.isItemChecked(position);
    final int selectionMode = lv.getChoiceMode();

    // The item we want to get the view for
    // --
    Item item = getItem(position);

    // Re-use the view if possible (recycle)
    // --
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = mInflator.inflate(R.layout.listview_row, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.txtDescription = (TextView) convertView.findViewById(R.id.description);
        holder.txtSessionCount = (TextView) convertView.findViewById(R.id.session_count);
        holder.listThumbnailImage = (ImageView) convertView.findViewById(R.id.list_image);
        holder.listStatusIndicatorImage = (ImageView) convertView.findViewById(R.id.status);
        holder.Checkbox = (InertCheckBox) convertView.findViewById(R.id.inertCheckBox);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.txtTitle.setText(item.getTitle());
    holder.txtDescription.setText(item.getDescription());
    holder.txtSessionCount.setText(item.getSessionCount());
    holder.listThumbnailImage.setImageBitmap((Bitmap) item.getThumbnailImage());        
    switch (selectionMode) {
    case ListView.CHOICE_MODE_NONE:
        holder.Checkbox.setVisibility(InertCheckBox.GONE);
        holder.listStatusIndicatorImage.setVisibility(ImageView.VISIBLE);
        holder.listStatusIndicatorImage.setImageBitmap((Bitmap) item.getListIndicatorImage());
        break;
    //case ListView.CHOICE_MODE_SINGLE: case ListView.CHOICE_MODE_MULTIPLE:
    default:
        holder.listStatusIndicatorImage.setVisibility(ImageView.GONE);
        holder.Checkbox.setVisibility(InertCheckBox.VISIBLE);
        holder.Checkbox.setButtonDrawable(R.drawable.checkbox);
        holder.Checkbox.setChecked(isChecked);
        break;
    }           


    return convertView;
}

@Override
public long getItemId(int position) {
    return getItem(position).getId();
}

@Override
public boolean hasStableIds() {
    return true;
}

项目类 - 上半部分;

public class Item implements Comparable<Item> {

private long id;
private String title;
private String description;
private String session_count;
private Bitmap listImage;
private Bitmap statusImage;

public Item(long id, String title, String description, String session_count, Bitmap listImage, Bitmap statusImage) {
    super();
    this.id = id;
    this.title = title;
    this.description = description;
    this.session_count = session_count;
    this.listImage = listImage;
    this.statusImage = statusImage;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

这是我调试日志中追踪项目移除的可视化展示。
07-23 22:59:14.910: D/drp(19104): Adapter Count is: 51
07-23 22:59:14.910: D/drp(19104): removing : 50 of 4-3 - Title: Test 50 - testing
07-23 22:59:14.910: D/drp(19104): removing : 49 of 4-2 - Title: Test 49 - testing
07-23 22:59:14.910: D/drp(19104): removing : 48 of 4-1 - Title: Test 48 - testing

如果我在MainActivity中注释掉“mMyListViewAdapter.remove(item);”这一行,就不会崩溃,并且日志似乎表明它正在按预期工作。有谁能看出我的错误导致了Index Out Of Bounds Exception?

此外,我正在使用SDK 4.0.4 API 15。

非常感谢,

保罗。

附加信息-完整的日志输出

        07-25 00:21:53.235: D/AbsListView(25952): Get MotionRecognitionManager
        07-25 00:21:53.270: D/dalvikvm(25952): GC_CONCURRENT freed 89K, 3% free 13027K/13383K, paused 1ms+2ms
        07-25 00:21:53.430: D/dalvikvm(25952): GC_CONCURRENT freed 207K, 4% free 13232K/13703K, paused 3ms+2ms
        07-25 00:21:53.630: D/CLIPBOARD(25952): Hide Clipboard dialog at Starting input: finished by someone else... !
        07-25 00:21:54.930: D/dalvikvm(25952): GC_FOR_ALLOC freed 189K, 4% free 13331K/13767K, paused 10ms
        07-25 00:21:54.930: I/dalvikvm-heap(25952): Grow heap (frag case) to 13.610MB for 408976-byte allocation
        07-25 00:21:54.940: D/dalvikvm(25952): GC_FOR_ALLOC freed 6K, 4% free 13724K/14215K, paused 9ms
        07-25 00:21:54.950: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 4% free 13724K/14215K, paused 9ms
        07-25 00:21:54.950: I/dalvikvm-heap(25952): Grow heap (frag case) to 13.994MB for 408976-byte allocation
        07-25 00:21:54.960: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 4% free 14124K/14663K, paused 9ms
        07-25 00:21:54.970: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 4% free 14124K/14663K, paused 9ms
        07-25 00:21:54.975: I/dalvikvm-heap(25952): Grow heap (frag case) to 14.384MB for 408976-byte allocation
        07-25 00:21:54.995: D/dalvikvm(25952): GC_CONCURRENT freed 0K, 4% free 14523K/15111K, paused 1ms+1ms
        07-25 00:21:55.005: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 4% free 14523K/15111K, paused 9ms
        07-25 00:21:55.005: I/dalvikvm-heap(25952): Grow heap (frag case) to 14.774MB for 408976-byte allocation
        07-25 00:21:55.020: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 5% free 14923K/15559K, paused 9ms
        07-25 00:21:55.030: D/dalvikvm(25952): GC_FOR_ALLOC freed <1K, 5% free 14923K/15559K, paused 9ms
        07-25 00:21:55.030: I/dalvikvm-heap(25952): Grow heap (frag case) to 15.165MB for 408976-byte allocation
        07-25 00:21:55.040: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 5% free 15322K/16007K, paused 10ms
        07-25 00:21:55.055: D/dalvikvm(25952): GC_FOR_ALLOC freed 0K, 5% free 15722K/16455K, paused 9ms
        07-25 00:21:55.110: D/dalvikvm(25952): GC_FOR_ALLOC freed 157K, 5% free 16145K/16903K, paused 9ms
        07-25 00:21:56.565: E/SKIA(25952): FimgApiStretch:stretch failed
        07-25 00:21:56.690: E/SKIA(25952): FimgApiStretch:stretch failed
        07-25 00:21:56.710: E/SKIA(25952): FimgApiStretch:stretch failed
        07-25 00:22:00.545: D/drp(25952): Adapter Count is: 51
        07-25 00:22:00.545: D/drp(25952): removing : 49 of 2-2 - Title: Test 49 - testing
        07-25 00:22:00.545: D/drp(25952): removing : 48 of 2-1 - Title: Test 48 - testing
        07-25 00:22:00.545: D/drp(25952): removing : 47 of 2-0 - Title: Test 47 - testing
        07-25 00:22:00.550: D/AndroidRuntime(25952): Shutting down VM
        07-25 00:22:00.550: W/dalvikvm(25952): threadid=1: thread exiting with uncaught exception (group=0x40c6f1f8)
        07-25 00:22:00.560: E/AndroidRuntime(25952): FATAL EXCEPTION: main
        07-25 00:22:00.560: E/AndroidRuntime(25952): java.lang.IndexOutOfBoundsException: Invalid index 48, size is 48
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at java.util.ArrayList.get(ArrayList.java:304)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at au.drp.mylistview.MyListViewAdapter.getItemId(MyListViewAdapter.java:107)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.AbsListView.confirmCheckedPositionsById(AbsListView.java:5956)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.AbsListView.handleDataChanged(AbsListView.java:5999)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.ListView.layoutChildren(ListView.java:1535)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.AbsListView.onLayout(AbsListView.java:2254)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.View.layout(View.java:11467)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewGroup.layout(ViewGroup.java:4237)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.View.layout(View.java:11467)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewGroup.layout(ViewGroup.java:4237)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.View.layout(View.java:11467)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewGroup.layout(ViewGroup.java:4237)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.View.layout(View.java:11467)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewGroup.layout(ViewGroup.java:4237)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.View.layout(View.java:11467)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewGroup.layout(ViewGroup.java:4237)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1721)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2678)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.os.Handler.dispatchMessage(Handler.java:99)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.os.Looper.loop(Looper.java:137)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at android.app.ActivityThread.main(ActivityThread.java:4514)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at java.lang.reflect.Method.invokeNative(Native Method)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at java.lang.reflect.Method.invoke(Method.java:511)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
        07-25 00:22:00.560: E/AndroidRuntime(25952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)

跟随这个教程可能会帮助你:http://www.quicktips.in/how-to-create-multi-select-listview-android-with-custom-adapter/ - Deepak Swami
感谢选择 selectionMode = lv.getChoiceMode()。我一直在寻找这个。 - CoolMind
6个回答

4

AbsListView中似乎存在一个错误,导致了这个问题。它可能会发生在任何AbsListView的子类中,包括ListViewGridView

在单选和多选模式下,ListView响应其适配器上的notifyDataSetChanged()调用,通过在confirmCheckedPositionsById()中验证所选项目的集合来进行操作。由于此时已从数据集中删除所选项目,因此适配器将抛出异常。值得注意的是,只有当适配器的hasStableIds()方法返回true时才会出现此问题。

粗略地说,这是相关路径:

  1. 您在ListView中选择一个或多个项目
  2. ListView更新其选定项目的列表
  3. 您单击删除按钮
  4. 该项(项)将从数据集中删除
  5. 您在适配器上调用notifyDataSetChanged(),并通知其观察者数据集已更改。 ListView是这些观察者之一。
  6. 下次重新绘制ListView时,它会看到适配器的通知并调用handleDataChanged()。此时,ListView仍然认为我们现在已删除的项目被选中并且在数据集中
  7. handleDataChanged()方法调用confirmCheckedPositionsById(),后者尝试使用过时的位置在适配器上调用getItemId()。如果删除的项目恰好靠近列表末尾,则该位置很可能超出数组的范围,适配器将抛出IndexOutOfBoundsException

两种可能的解决方法如下:

  • 根据其他答案中的提示,每次数据集更改时都要创建一个新的适配器。这样做的不幸影响是会丢失当前滚动位置,除非手动保存和恢复。

  • 在调用适配器的notifyDataSetChanged()之前,在ListView(或GridView)上调用clearChoices()来清除所选项目。无论如何,所选项目都将被删除,因此失去当前选择状态不太可能成为问题。这种方法将保留滚动位置,并应该防止在更新列表时出现闪烁。


acj,你是正确的,当在过时的位置上调用confirmCheckPositionsByid()时会出现错误。然而,我发现如果我调用clearChoices(),那么ActionMode#finish就无法退出多选模式。 - Peter Tran
@PeterTran 很有趣。在我的工作中(我使用的是4.0+),调用clearChoices()然后调用ActionMode#finish()退出多选模式是可行的。如果你找到了其他解决方法,请再次发布。 - acj
清除选择!!!没问题!!!但不要忘记设置 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 跟进! - YETI

4

confirmCheckedPositionsById中的错误(acj答案中的第7个弹药)会导致getItemId在陈旧位置上被调用。但是,它将再次被调用以使用正确的位置来刷新布局。当我遇到这个问题时,我像这样更新了自定义适配器的getItemId

@Override
public long getItemId(int position) {
    return position < getCount() ? getItem(position).getId() : -1;
}

谢谢,这对我有用!我不知道,但我们已经是2015年了,confirmCheckedPositionsById仍然存在bug。 - Aitch
嗨,2015年!!现在已经是2021年了,我仍然面临着同样的错误:@ - Adam Burley

3

好的,我解决了这个问题!太棒了!

为了防止IndexOutOfBoundsException异常,我需要重置列表视图适配器,并刷新列表视图内容。所以神奇的一行是:

 listView.setAdapter(mMyListViewAdapter);

然而,我相信在处理列表视图时,使用这种方法并不是最佳实践,最好的方式是更新与列表视图相关连的适配器的内容。但是我不太确定应该如何进行操作?

不管怎样,这里是我更新后的删除方法代码:

private void removeItems() {
    final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();

    if (checkedItems != null) {
        final int checkedItemsCount = checkedItems.size();

        // Lets get the position of the view to scroll to before the first checked 
        // item to restore scroll position
        //          
        int topPosition = checkedItems.keyAt(0) - 1;            

        listView.setAdapter(null);
        for (int i = checkedItemsCount - 1; i >= 0 ; i--) {
            // This tells us the item position we are looking at
            // --
            final int position = checkedItems.keyAt(i);

            // This tells us the item status at the above position
            // --
            final boolean isChecked = checkedItems.valueAt(i);
            if (isChecked) {
                Item item = mMyListViewAdapter.getItem(position);
                mMyListViewAdapter.remove(item);
                //mMyListViewAdapter.notifyDataSetChanged();

            }               
        }
        listView.setAdapter(mMyListViewAdapter);
        //if topPosition is -1 then item zero is positioned by default.
        listView.setSelection(topPosition);
    }
}

0

使用AlertDialog.Builder进行完全自定义的筛选:

((TextView) TabBarWithCustomStack.vPublic.findViewById(R.id.tv_edit)) .setVisibility(View.GONE);
    class DialogSelectionClickHandler implements
            DialogInterface.OnMultiChoiceClickListener {
        public void onClick(DialogInterface dialog, int clicked,
                boolean selected) {
            Log.i("ME", hosts.toArray()[clicked] + " selected: " + selected);
        }
    }

    class DialogButtonClickHandler implements
            DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int clicked) {
            switch (clicked) {
            case DialogInterface.BUTTON_POSITIVE:
                filteredList.clear();
                statusesSelected.clear();

                for (int i = 0; i < statusesStringArray.length; i++) {
                    // Log.i( "ME", statuses.toArray()[ i ] + " selected: "
                    // + isSelectedStatuses[i] );

                    if (isSelectedStatuses[i] == true) {

                        if (statuses.get(i).toString()
                                .equalsIgnoreCase("Accepted")) {
                            statusesSelected.add("1");
                        } else if (statuses.get(i).toString()
                                .equalsIgnoreCase("Rejected")) {
                            statusesSelected.add("2");
                        } else if (statuses.get(i).toString()
                                .equalsIgnoreCase("Pending")) {
                            statusesSelected.add("3");
                        }

                    }
                    isSelectedStatuses[i] = false;
                }

                Calendar currentCalender = Calendar.getInstance(Locale
                        .getDefault());

                Date currentDate = new Date(
                        currentCalender.getTimeInMillis());

                if (listSelected == 0) {
                    for (int j = 0; j < arr_BLID.size(); j++) {
                        if (Helper.stringToDate(
                                arr_BLID.get(j).getStart_ts().toString(),
                                Helper.SERVER_FORMAT).after(currentDate)) {
                            if (statusesSelected.contains(arr_BLID.get(j)
                                    .getStatus())) {
                                filteredList.add(arr_BLID.get(j));
                            }
                        }
                    }

                } else {
                    for (int j = 0; j < arr_BLID.size(); j++) {
                        if (currentDate.after(Helper.stringToDate(arr_BLID
                                .get(j).getStart_ts().toString(),
                                Helper.SERVER_FORMAT))) {
                            if (statusesSelected.contains(arr_BLID.get(j)
                                    .getStatus())) {
                                filteredList.add(arr_BLID.get(j));
                            }
                        }
                    }

                }

                lvBeeps.setAdapter(new EventsAdapter(ctx));

                break;

            case DialogInterface.BUTTON_NEGATIVE: {

                currentCalender = Calendar.getInstance(Locale.getDefault());

                currentDate = new Date(currentCalender.getTimeInMillis());

                if (listSelected == 0) {

                    filteredList.clear();
                    for (int i = 0; i < arr_BLID.size(); i++) {
                        if (Helper.stringToDate(
                                arr_BLID.get(i).getStart_ts().toString(),
                                Helper.SERVER_FORMAT).after(currentDate)) {
                            filteredList.add(arr_BLID.get(i));
                        }

                        if (i < isSelectedStatuses.length) {
                            isSelectedStatuses[i] = false;
                        } else {
                            continue;
                        }
                    }

                    lvBeeps.setAdapter(new EventsAdapter(ctx));
                } else {

                    filteredList.clear();
                    for (int i = 0; i < arr_BLID.size(); i++) {
                        if (currentDate.after(Helper.stringToDate(arr_BLID
                                .get(i).getStart_ts().toString(),
                                Helper.SERVER_FORMAT))) {
                            filteredList.add(arr_BLID.get(i));
                        }

                        if (i < isSelectedStatuses.length) {
                            isSelectedStatuses[i] = false;
                        } else {
                            continue;
                        }

                    }

                    lvBeeps.setAdapter(new EventsAdapter(ctx));

                }
            }
                break;
            }
        }
    }

    btnHost = (Button) view.findViewById(R.id.btnHost);
    btnHost.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // ((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.tvDialogTitle)).setText("Hosts");

            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.tvDialogTitle)).getText());

            final LayoutInflater inflater123 = (LayoutInflater) getActivity()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View view123 = inflater123.inflate(
                    R.layout.event_filter_title, null);

            // Log.d( "Dialog object",
            // " static made dialog in view123--> "+view123);
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)view123.findViewById(R.id.cbSelectAll)));
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)inflater.inflate(R.layout.event_filter_title,
            // (ViewGroup) ((AlertDialog)
            // BeepsFragment.dialog).getCurrentFocus(),
            // true).findViewById(R.id.cbSelectAll)));
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)(((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.cbSelectAll))));
            hostsDialog = new AlertDialog.Builder(getActivity())
                    .setCustomTitle(/*
                                     * Html.fromHtml(
                                     * "<b><font color=\"purple\">  Host</font></b>"
                                     * )
                                     */view123)
                    .setIcon(
                            getActivity().getResources().getDrawable(
                                    R.drawable.add_host))
                    .setMultiChoiceItems(hostsStringArray, isSelectedHosts,
                            new OnMultiChoiceClickListener() {

                                // android.content.DialogInterface.OnShowListener
                                // ocl = new
                                // DialogInterface.OnShowListener() {
                                //
                                // @Override
                                // public void onShow(DialogInterface
                                // dialog) {
                                // // TODO Auto-generated method stub
                                // BeepsFragment.dialog = dialog;
                                // }
                                // };

                                public void onClick(DialogInterface dialog,
                                        int clicked, boolean selected) {

                                    boolean all = true;
                                    Log.i("ME", hosts.toArray()[clicked]
                                            + " selected: " + selected);

                                    // for (int i = 0; i <
                                    // isSelectedHosts.length; i++ ){
                                    //
                                    // if(isSelectedHosts[i]==true){
                                    // all = true;
                                    // }else{
                                    // all = false;
                                    // }
                                    // }
                                    //
                                    // Log.i( "ME", all + " selected:--- "
                                    // +((CheckBox)view123.findViewById(R.id.cbSelectAll)));
                                    //
                                    // if(all = true){
                                    // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(true);
                                    // }else{
                                    // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(false);
                                    // }

                                    Log.d("Dialog object",
                                            " static made dialog --> "
                                                    + BeepsFragment.dialog
                                                    + " from parameter dialog --> "
                                                    + dialog);

                                }

                            })
                    .setPositiveButton(
                            Html.fromHtml("<b><font color=\"purple\">Apply Filter</font></b>"),
                            new DialogButtonClickHandler() {

                                // android.content.DialogInterface.OnShowListener
                                // ocl = new
                                // DialogInterface.OnShowListener() {
                                //
                                // @Override
                                // public void onShow(DialogInterface
                                // dialog) {
                                // // TODO Auto-generated method stub
                                // BeepsFragment.dialog = dialog;
                                // }
                                // };

                                public void onClick(DialogInterface dialog,
                                        int clicked) {
                                    switch (clicked) {
                                    case DialogInterface.BUTTON_POSITIVE:

                                        filteredList.clear();
                                        hostsSelected.clear();

                                        for (int i = 0; i < hostsStringArray.length; i++) {
                                            Log.i("ME",
                                                    hosts.toArray()[i]
                                                            + " selected: "
                                                            + isSelectedHosts[i]
                                                            + "\n\n\t hostStringArray-->"
                                                            + hostsStringArray[i]);

                                            if (isSelectedHosts[i] == true) {

                                                hostsSelected.add(hosts
                                                        .get(i));
                                                isSelectedHosts[i] = false;
                                            }
                                            // isSelectedHosts[i] = false;
                                        }

                                        Calendar currentCalender = Calendar
                                                .getInstance(Locale
                                                        .getDefault());

                                        Date currentDate = new Date(
                                                currentCalender
                                                        .getTimeInMillis());

                                        if (listSelected == 0) {
                                            for (int j = 0; j < arr_BLID
                                                    .size(); j++) {
                                                if (Helper
                                                        .stringToDate(
                                                                arr_BLID.get(
                                                                        j)
                                                                        .getStart_ts()
                                                                        .toString(),
                                                                Helper.SERVER_FORMAT)
                                                        .after(currentDate)) {
                                                    if (hostsSelected
                                                            .contains(arr_BLID
                                                                    .get(j)
                                                                    .getHost_name())) {
                                                        filteredList
                                                                .add(arr_BLID
                                                                        .get(j));
                                                    }
                                                    if (hostsSelected
                                                            .contains("Me"))
                                                        if (BeepApplication
                                                                .getSelfId() == arr_BLID
                                                                .get(j)
                                                                .getHost_id())
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                }
                                            }

                                        } else {
                                            for (int j = 0; j < arr_BLID
                                                    .size(); j++) {
                                                if (currentDate.after(Helper
                                                        .stringToDate(
                                                                arr_BLID.get(
                                                                        j)
                                                                        .getStart_ts()
                                                                        .toString(),
                                                                Helper.SERVER_FORMAT))) {
                                                    if (hostsSelected
                                                            .contains(arr_BLID
                                                                    .get(j)
                                                                    .getHost_name())) {
                                                        filteredList
                                                                .add(arr_BLID
                                                                        .get(j));
                                                    }
                                                    if (hostsSelected
                                                            .contains("Me"))
                                                        if (BeepApplication
                                                                .getSelfId() == arr_BLID
                                                                .get(j)
                                                                .getHost_id())
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                }
                                            }

                                        }
                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));
                                        break;
                                    }
                                }

                            })
                    .setNegativeButton(
                            Html.fromHtml("<b><font color=\"purple\">Remove Filter</font></b>"),
                            new DialogButtonClickHandler() {

                                public void onClick(
                                        final DialogInterface dialog,
                                        int clicked) {

                                    Calendar currentCalender = Calendar
                                            .getInstance(Locale
                                                    .getDefault());

                                    Date currentDate = new Date(
                                            currentCalender
                                                    .getTimeInMillis());

                                    if (listSelected == 0) {

                                        filteredList.clear();
                                        for (int i = 0; i < arr_BLID.size(); i++) {
                                            if (Helper.stringToDate(
                                                    arr_BLID.get(i)
                                                            .getStart_ts()
                                                            .toString(),
                                                    Helper.SERVER_FORMAT)
                                                    .after(currentDate)) {
                                                filteredList.add(arr_BLID
                                                        .get(i));
                                                if (i < isSelectedHosts.length) {
                                                    isSelectedHosts[i] = false;
                                                } else {
                                                    continue;
                                                }
                                            }
                                        }

                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));
                                    } else {

                                        filteredList.clear();
                                        for (int i = 0; i < arr_BLID.size(); i++) {
                                            if (currentDate.after(Helper
                                                    .stringToDate(
                                                            arr_BLID.get(i)
                                                                    .getStart_ts()
                                                                    .toString(),
                                                            Helper.SERVER_FORMAT))) {
                                                filteredList.add(arr_BLID
                                                        .get(i));
                                                if (i < isSelectedHosts.length) {
                                                    isSelectedHosts[i] = false;
                                                } else {
                                                    continue;
                                                }
                                            }
                                        }

                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));

                                    }
                                }
                            });

            final AlertDialog dlg = hostsDialog.create();

            dlg.show();

            ((TextView) view123.findViewById(R.id.tvDialogTitle))
                    .setText("Hosts");

            ((CheckBox) view123.findViewById(R.id.cbSelectAll))
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub

                            if (isChecked == true) {

                                ListView list = dlg.getListView();
                                for (int i = 0; i < list.getCount(); i++) {

                                    isSelectedHosts[i] = true;
                                    list.setItemChecked(i, true);

                                }

                            }

                            else if (isChecked == false) {

                                ListView list = dlg.getListView();
                                for (int j = 0; j < list.getCount(); j++) {

                                    isSelectedHosts[j] = false;
                                    list.setItemChecked(j, false);
                                }
                                // }
                            }

                        }
                    });

        }
    });

    btnStatus = (Button) view.findViewById(R.id.btnStatus);
    btnStatus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            LayoutInflater inflater123 = (LayoutInflater) getActivity()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view123 = inflater123.inflate(R.layout.event_filter_title,
                    null);

            statusDialog = new AlertDialog.Builder(getActivity())
                    .setIcon(
                            getActivity().getResources().getDrawable(
                                    R.drawable.add_host))
                    .setCustomTitle(/*
                                     * Html.fromHtml(
                                     * "<b><font color=\"purple\">  Status</font></b>"
                                     * )
                                     */view123)
                    .setIcon(
                            getActivity().getResources().getDrawable(
                                    R.drawable.add_host))
                    .setMultiChoiceItems(statusesStringArray,
                            isSelectedStatuses,
                            new DialogSelectionClickHandler())
                    .setPositiveButton(
                            Html.fromHtml("<b><font color=\"purple\">Apply Filter</font></b>"),
                            new DialogButtonClickHandler() {

                            })
                    .setNegativeButton(
                            Html.fromHtml("<b><font color=\"purple\">Remove Filter</font></b>"),
                            new DialogButtonClickHandler());

            final AlertDialog dlg = statusDialog.create();

            dlg.show();

            ((TextView) view123.findViewById(R.id.tvDialogTitle))
                    .setText("Status");
            ((CheckBox) view123.findViewById(R.id.cbSelectAll))
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub

                            if (isChecked == true) {

                                ListView list = dlg.getListView();
                                for (int i = 0; i < list.getCount(); i++) {

                                    isSelectedStatuses[i] = true;
                                    list.setItemChecked(i, true);

                                }

                            }

                            else if (isChecked == false) {

                                ListView list = dlg.getListView();
                                for (int j = 0; j < list.getCount(); j++) {

                                    isSelectedStatuses[j] = false;
                                    list.setItemChecked(j, false);
                                }
                                // }
                            }

                        }
                    });

        }
    });

0

这是一个已记录的错误,请为其投票:

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

我的使用情况是将ListView配置为ListView.CHOICE_MODE_SINGLE,我尝试了@Peter Tran的建议,但没有成功。这是对我有效的解决方法:

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

我的测试是手动选择列表中的最后一项(count-1)。我省略了当count==0时的处理,但那可能是需要的。我观察到println()总是打印已删除行的索引。myAdapter.deleteRow()通知监听器数据已更改,这表明ListView没有正确更新其已勾选的索引。我已经尝试过将hasStableIds()从自定义适配器中返回true和false,并获得了相同的结果。


很不幸,这个错误票据因为长时间没有得到谷歌的关注而被关闭了 :( 否则我会投票支持它的。现在已经是2021年了,我仍然面临着这个问题 :( - Adam Burley

-1

编辑为:

for (int i = checkedItemsCount-1; i >= 0 ; i--) {
                                          ^^^^^

而不是

for (int i = checkedItemsCount-1; i >= 0 ; --i) {

谢谢您的建议,但不幸的是我仍然遇到了相同的数组越界异常。还有其他建议吗? - luthepa1
请发布完整的 Log-Cat 显示错误。 - Chintan Raghwani
你有 ArrayList.java 文件吗?如果有,那么在该文件的第 251 行的代码是什么? - Chintan Raghwani
这不是我编写的文件。假设它是在编译时从SDK链接的。当我坐在电脑前时,我可以仔细看一下。 - luthepa1
这个答案中的代码更改不应该有任何影响。前增量和后增量只有在i----i表达式的值被使用时才有关系,而当它是for循环的第三部分时,它并没有被使用。 - LarsH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接