0

我正在尝试删除选定的复选框。在下面的代码之后我不知道该怎么做:我正在使用一个简单的光标适配器。我还想知道如何选择和取消选择所有复选框。我已经研究了一切,但找不到任何答案。我被困了3天。另外,我不确定下面的代码是否是我需要的正确路径。

public class SecondCheckedListView extends ListActivity {

 private Cursor mCursor;
 private Long mRowId;
DBAdapter db = new DBAdapter(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondcheckedlistview);
    db.open();
    fillData();




 }

private void fillData() {
    mCursor = db.getAllContacts();
   startManagingCursor(mCursor);
   String[] from = new String[]{DBAdapter.KEY_FIRSTNAME, DBAdapter.KEY_LASTNAME}; 
   int[] to = new int[]{R.id.textView1, R.id.textView2};
   SimpleCursorAdapter d = 
           new SimpleCursorAdapter(this, R.layout.rowcheckedlistview, mCursor, from, to);
       setListAdapter(d);


}

public class MyAdapter extends SimpleCursorAdapter {

    private final Context mContext;
    private final int mLayout;
    private final Cursor mCursor;
    private final int mNameIndex;
    private final int mIdIndex;
    private final LayoutInflater mLayoutInflater;

    private final class ViewHolder {
            public TextView firstname;
            public TextView lastname;
            public CheckBox cBox;
    }
    public MyAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.mContext = context;
        this.mLayout = layout;
        this.mCursor = c;
        this.mNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
        this.mIdIndex = mCursor.getColumnIndex(DBAdapter.ROW_ID);
        this.mLayoutInflater = LayoutInflater.from(mContext);

    }

    @Override
    public View getView(final int pos, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(pos)) {
            ViewHolder viewHolder;
            if(convertView == null) {
                convertView = mLayoutInflater.inflate(mLayout, null);

                viewHolder = new ViewHolder();
                viewHolder.firstname = (TextView)     convertView.findViewById(R.id.textView1);
                viewHolder.lastname = (TextView) convertView.findViewById(R.id.textView2);
                viewHolder.cBox = (CheckBox) convertView.findViewById(R.id.bcheck);

                convertView.setTag(viewHolder);

            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String firstname = mCursor.getString(mNameIndex);
            String rowId = mCursor.getString(mIdIndex);


}
        return convertView;
    }



    }

}

4

1 回答 1

0

你是什​​么意思“删除”?您想从列表视图中删除复选框吗?您可能需要执行类似 viewHolder.RemoveView(cBox); 的操作。

如果您想选择/取消所有复选框,您可能需要基于其他一些按钮或复选框。我用另一个复选框来做到这一点,如下所示:

private OnClickListener setSelectAllListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        selectAllTouched = true;
        if (!selectAllCheck.isChecked()) {
           selectAll = false;
           studs.notifyDataSetChanged();
         }
        else {
           selectAll = true;
           studs.notifyDataSetChanged();
        }
    }
}; 

然后在我的自定义适配器中对列表视图中的每个复选框进行处理,我使用这个:

 if (selectAllTouched) {
            if(selectAll){
                holder.here.setChecked(true);
                itemCheckedHere.set(pos, true);
                int who= new Integer(holder.text2.getText().toString());
                mDbHelper.updateAttend(who, classnum, ATTEND, 1, attendDate );
            }
            else{
                holder.here.setChecked(false);
                itemCheckedHere.set(pos, false);
                int who = new Integer(holder.text2.getText().toString());
                mDbHelper.updateAttend(who, classnum, ATTEND, 0, attendDate );
            }
        }

我使用了一个稀疏数组 itemCheckedHere 来保存所有复选框的值,因此滚动不会改变结果视图上的值。

于 2012-08-29T10:43:02.527 回答