ListView中使用CursorAdapter的复选框

时间:2014-04-13 21:20:48

标签: android listview checkbox

我的MainActivity包含ListView和一个启动DetailsActivity的菜单项。 DetailsActivity可以向填充ListView的数据库添加项目。以下是对我造成问题的用例的描述:

  1. 开始MainActivity
  2. 检查列表中的一些项目。
  3. 启动DetailsActivity
  4. 将项目添加到数据库中。
  5. 按后退按钮返回MainActivity
  6. 在此序列结束时,即使我尝试恢复其状态,也不会保留步骤2中的任何检查。

    MainActivity.onPause()和MainActivity.onResume():

    @Override
    public void onPause() {
        super.onPause();
    
        this.savedSelection = this.adapter.getSelection();
    }
    @Override
    public void onResume() {
        super.onResume();
    
        // restore default header state
        CheckedTextView headerCheck = (CheckedTextView) this.headerView
                .findViewById(R.id.checkmark);
        headerCheck.setChecked(false);
    
        boolean[] newSelection = new boolean[this.adapter.getCount()];
    
        if (this.savedSelection != null) {
            // copy old selection array into new selection array
            int numSelected = 0;
            for (int i = 0; i < this.savedSelection.length; i++) {
                newSelection[i] = this.savedSelection[i];
    
                if (newSelection[i]) {
                    numSelected++;
                }
    
                View row = this.listView.getChildAt(i + 1);
    
                if (row != null) {
                    CheckedTextView checkbox = (CheckedTextView) row
                            .findViewById(R.id.checkmark);
                    checkbox.setChecked(newSelection[i]);
    
                    TextView playerText = (TextView) row
                            .findViewById(R.id.player_name_text_view);
                    Log.d(TAG, playerText.getText().toString() + " checked? "
                            + checkbox.isChecked());
                }
            }
    
            // restore header state
            if (numSelected == newSelection.length) {
                headerCheck.setChecked(true);
            }
    
        }
    
        // restore state
        this.adapter.setSelection(newSelection);
    }
    

    MyAdapter.getView():

    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
    
        CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.checkmark);
        final Activity curActivity = (Activity) this.context;
    
        // restore selection
        if (this.selection != null) {
            ctv.setChecked(this.selection[position]);
        }
    
        // set listener
        ctv.setOnClickListener(new OnClickListener() {
    
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                CheckedTextView cview = (CheckedTextView) v
                        .findViewById(R.id.checkmark);
                cview.toggle();
                BaseballCardAdapter.this.selection[position] = cview
                        .isChecked();
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    curActivity.invalidateOptionsMenu();
                }
    
            }
    
        });
    
        return v;
    }
    

1 个答案:

答案 0 :(得分:0)

我认为如果使用savedInstanceState会更好,并在onCreate方法中设置this.savedSelection。像这样:

@Override
protected void onSaveInstanceState (Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putBooleanArray(SOME_CONSTANT_STRING,this.savedSelection);
}

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (savedInstanceState!=null) {
      this.savedSelection=savedInstanceState.getBooleanArray(SOME_CONSTANT_STRING);
   }
}

我相信你的其他代码应该可行。