安卓:list.getCheckedItemPositions()始终返回null [英] Android: list.getCheckedItemPositions() always returns null

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

问题描述

我通过扩展SimpleCursorAdapter创建一个自定义的ListView。其结果是图像+ CheckedTextView(文字+复选框)。

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

在我有错复选框得到检查的问题(<一href="http://stackoverflow.com/questions/4010623/android-click-on-listitem-checks-wrong-checkbox">see这里),我不得不删除 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 从的onCreate

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

现在我点击一个列表项线,它检查权复选框。

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

我想要做的是使一个LinearLayout.VISIBLE时1个或多个复选框被选中,并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.

下面是code:

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

我有两个问题是:

I have two issues with this:

  1. 如果我检查2个或更多的复选框,然后取消选择只有一个,在LinearLayout中消失,这意味着 SparseBooleanArray拉拉=((ListView控件)母公司).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中走了之后,一切都乱七八糟再次,就像在<一个HREF =htt​​p://stackoverflow.com/questions/4010623/android-click-on-listitem-checks-wrong-checkbox>我的previous问题。

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.

鸭preciate任何帮助!

Appreciate any help!

推荐答案

你为什么要期望getCheckedItemPositions()来如果没有选中的项返回null?它看起来像你的选择方式是错误的。

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

A SparseBooleanArray这将返回true每次调用得到(INT位置)的位置是在列表中的位置,或NULL,如果选择模式设置为CHOICE_MODE_NONE。

数组应该只为空,如果是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;
}

更新

我是pretty的确保它有做这行

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

您获取视图,然后重新分配,以充气。我有点不知所措,虽然在到底为什么它会给你exeriencing的行为。你可能也想试试 bindView()方法,虽然我不知道为什么。而不是使用默认的Andr​​oid布局(一你给的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.

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

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