Android:list.getCheckedItemPositions() 总是返回 null [英] Android: list.getCheckedItemPositions() always returns null

查看:13
本文介绍了Android:list.getCheckedItemPositions() 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过扩展 SimpleCursorAdapter 创建了一个自定义 ListView.结果是 IMAGE + CheckedTextView (Text + Checkbox).

I've created a custom ListView by extending SimpleCursorAdapter. The result is IMAGE + CheckedTextView (Text + Checkbox).

在我遇到错误复选框被检查的问题后(​​见这里) 我不得不从 onCreate 中删除 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);.

After I had issues with the wrong checkboxes getting checked (see here) I had to remove lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); from onCreate.

现在我点击 ListItem 行,它会选中正确的复选框.

Now I click on a ListItem line and it checks the right checkbox.

我想要做的是在选中 1 个或多个复选框时创建 LinearLayout.VISIBLE 并在未选中任何复选框时创建 LinearLayout.GONE.

What I'm trying to do is make a LinearLayout.VISIBLE when 1 or more checkboxes are checked AND make LinearLayout.GONE when no checkbox is checked.

代码如下:

lv.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);

if (!markedItem.isChecked()) {
 markedItem.setChecked(true);

 //show bottom control panel
 findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.VISIBLE);


} else {
 markedItem.setChecked(false);

 SparseBooleanArray lala = ((ListView) parent).getCheckedItemPositions();

 //if no checkbox is checked, hide bottom control panel
 if (lala == null) {
  findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.INVISIBLE);
 }
}
view.refreshDrawableState();
//showSortBtn(markedItem);

}});

这是我将 listView 绑定到自定义适配器的方式:

This is how I bind the listView to the custom adapter:

projection = new String[] { Browser.BookmarkColumns._ID,
                Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.URL };
        displayFields = new String[] { Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.URL };
        int[] displayViews = new int[] { android.R.id.icon, android.R.id.text1 };

        cur = managedQuery(BOOKMARKS_URI, projection,
                Browser.BookmarkColumns.BOOKMARK + " == 1", null, Browser.BookmarkColumns.VISITS + " DESC");

        setListAdapter(new ImageCursorAdapter(this,
                android.R.layout.simple_list_item_single_choice, cur,
                displayFields, displayViews));

我有两个问题:

  1. 如果我选中 2 个或更多复选框,然后只取消选中一个,则 LinearLayout 消失,这意味着 SparseBooleanArray lala = ((ListView) parent).getCheckedItemPositions(); 为空.如果还有其他复选框被选中,为什么它为空?

  1. if I check 2 or more checkboxes and then uncheck only one, the LinearLayout disappears which means SparseBooleanArray lala = ((ListView) parent).getCheckedItemPositions(); is null. why is it null if there are still other checkboxes checked?

如果我将 findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.INVISIBLE); 切换到 findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.GONE));(就像我想做的那样)第一个复选框被正确选中,在 LinearLayout 消失之后,一切又变得一团糟,就像在 我之前的问题.

if I switch findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.INVISIBLE); to findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.GONE); (like I want to do) the first checkbox is being checked correctly and after the LinearLayout is GONE, everything is a mess again, just like on my previous question.

感谢任何帮助!

推荐答案

如果没有选中的项目,你为什么期望 getCheckedItemPositions() 返回 null?看来你的选择模式不对

Why are you expecting getCheckedItemPositions() to return null if there aren't checked items? It looks like your choice mode is wrong

一个 SparseBooleanArray,它会在每次调用 get(int position) 时返回 true,其中 position 是列表中的一个位置,如果选择模式设置为 CHOICE_MODE_NONE,则返回 null.

如果它是choice_mode_none,数组应该只为空.

The array should only be null if it's choice_mode_none.

为什么不尝试这样的事情?

Why not try something like this?

if (((ListView) parent).getCheckedItemPositions().size() > 0){

    findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.VISIBLE);
else{

    findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.GONE;
}

更新

我很确定它与这条线有关

I'm pretty sure it has to do with this line

 View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.image_list, null);
        }

您获得视图,然后将视图重新分配给充气机.我有点不知所措,但究竟为什么它会给你正在经历的行为.您可能还想尝试 bindView() 方法,但我不确定为什么.也许不是使用默认的 android 布局(您提供给 setAdapter 的布局,而是创建自己的布局.很明显,对于将哪个视图分配给哪个布局感到困惑.

You get the view, then reassign the view to the inflater. I'm kind of at a loss though at exactly why it would give the behavior you're exeriencing. You might also want to try the bindView() method though I'm not sure why. Maybe instead of using the default android layout (the one you're giving to the setAdapter, make your own layout. It clearly is getting confused as to which view is assigned to which layout.

这篇关于Android:list.getCheckedItemPositions() 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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