Android:过滤列表视图时检查了错误的项目 [英] Android: Wrong item checked when filtering listview

查看:15
本文介绍了Android:过滤列表视图时检查了错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与此问题相同的问题:错误在android中过滤ListView时检查项目

I'm suffering from the same issue as this question: Wrong item checked when filtering ListView in android

正如上面的问题所建议的,我有一个 Hashset 保存所有 selectedIds,但是当光标重新填充选中的项目时,我不知道如何使用它.

As suggested in the above question, I have an Hashset holding all the selectedIds, but I can't figure out how to use it when the cursor is repopulating the checked items.

我的问题只是表面问题 - 例如:

My issue is only cosmetic - for example:

  • Facebook"位于未过滤列表中的第 5 位.
  • 用户搜索face",过滤列表中只有Facebook"出现在第 1 个位置.
  • 用户选中Facebook"并返回未过滤列表.
  • 选中的项目是列表中的第 1 个项目,而不是Facebook"(位于第 5 个).

注意:除了这个问题,其他一切都很好.比如delete"会删除正确的item,因为我是用selectedIds来做的(即使勾选的item有错).

Note: Except this issue, everything else works great. For example, "delete" will delete the right items because I use the selectedIds to do it (even if the checked items are wrong).

单击列表项:

OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                //gets the Bookmark ID of selected position
                    Cursor cursor = (Cursor)parent.getItemAtPosition(position);
                    String bookmarkID = cursor.getString(0);

                    boolean currentlyChecked = checkedStates.get(position);
                    checkedStates.set(position, !currentlyChecked);

                    if (!selectedIds.contains(bookmarkID)) {

                        selectedIds.add(bookmarkID);
                        selectedLines.add(position);

                    } else {

                        selectedIds.remove(bookmarkID);
                        selectedLines.remove(position);


                        }

            }
        };

在光标内部:- 这就是问题所在.

这会重新填充选中的项目 - 问题是它是按位置 (pos) 进行的,过滤列表中项目的正确位置是什么,而不是它在未过滤列表中的位置 - 导致错误标记的项目.

This repopulates the checked items - the problem is it does it by position (pos) and what was the right position of the item in the filtered list, is not its position in the unfiltered list - resulting in a wrongly marked item.

CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
markedItem.setChecked(checkedStates.get(pos));

非常感谢您的帮助!

推荐答案

解决了问题.正如我添加到我的问题中所建议的那样,当您填充复选框时,另一个 List/Hashset 应确定是否将项目标记为已选中.

Solved the issue. As suggested in the question I added to mine, when you populate the checkboxes, the other List/Hashset should determine whether to mark item as checked or not.

使用我的代码:

//first you will need to get the current item ID
String bookmarkID = cursor.getString(0);

//Then, check if the current ID is in the selectedIds hash/list
//If true - mark current item view as checked, else - uncheck.
        CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
        if (selectedIds.contains(new String(bookmarkID))) {
            markedItem.setChecked(true);

        } else {
            markedItem.setChecked(false);
}

真的希望这对任何人都有帮助!:)

Really hope this will help anyone! :)

这篇关于Android:过滤列表视图时检查了错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆