ListView和Checkbox - 未在教程中勾选复选框

时间:2012-09-01 19:18:22

标签: android cursor simplecursoradapter

过去几个小时我一直在阅读教程和论坛帖子,试图获得一个简单的复选框列表视图。

我理解(我相信)我需要如何在列表中维护我的复选框的状态(在我的情况下是一张地图)。

我确信这是一件非常简单的事情,但我的bindView方法中没有将复选框设置为true或false。这是代码

public void bindView(View view, Context context, Cursor cursor) {
        final int rowId = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));

        familyText = (TextView)view.findViewById(R.id.contacts_row_family_name);
        markedBox = (CheckBox)view.findViewById(R.id.contacts_row_check);
        familyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));



        view.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                Log.d("OnClick","Row clicked");
                boolean currentlyChecked;

                if(checkedState.size() >= rowId){
                    currentlyChecked = checkedState.get(rowId);
                    checkedState.put(rowId, !currentlyChecked);
                }else{
                    currentlyChecked = false;
                    checkedState.put(rowId, !currentlyChecked);
                }

                markedBox.setChecked(checkedState.get(rowId));

            }

        });

        setProgressBarIndeterminateVisibility(false);


    }

这是checkedState的声明。这是我的活动的成员。

private Map<Integer, Boolean> checkedState = new HashMap<Integer,Boolean>();

据我所知,我应该在该行上有一个监听器(这可行,因为我的日志消息正确打印)但复选框不会改变。

1 个答案:

答案 0 :(得分:0)

更新

我错误地检查了我的复选框。正在检查错误的一个。我将结束这个问题。