在bindView中勾选复选框

时间:2014-07-17 08:49:53

标签: android listview checkbox

我试图勾选行中的checbox,其中检索的参数等于" 1"在CustomCursorAdapter中:

更新:

public ImageCursorAdapter(Context context, int layout, Cursor cursor,
            String[] from, int[] to) {
        super(context, layout, cursor, from, to);
        this.layout = layout;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.cursor = cursor;
        itemNames.clear();
        for (int x = 0; x < this.getCount(); x++) {

            itemNames.add(x, null);
        }

        Log.d("111adapter", "itemNames Array in ImageCursorAdapter at start = " + String.valueOf(itemNames));

        itemChecked.clear();
        for (int i = 0; i < this.getCount(); i++) {

            itemChecked.add(i, false);
        }

        Log.d("111adapter", "itemChecked Array in ImageCursorAdapter at start = " + String.valueOf(itemChecked));

    }



    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }


    @Override
    public void bindView(View view, Context context, final Cursor cursor) {

        super.bindView(view, context, cursor);

        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(cursor.getString(cursor.getColumnIndexOrThrow("rentities_description")));

        ImageView imageView = (ImageView) view.findViewById(R.id.img);
        String iconname = cursor.getString(cursor.getColumnIndexOrThrow("rentities_icon"));

        Log.d("111adapter", "iconname in ImageCursorAdapter = " + iconname);
        Bitmap bmp = BitmapFactory.decodeFile(iconname);
        imageView.setImageBitmap(bmp);

        final CheckBox cBox = (CheckBox) view.findViewById(R.id.checkBox1);

        final int pos = cursor.getPosition();

        Log.d("111adapter", "pos in ImageCursorAdapter = " + String.valueOf(pos));

        final String entity_title = cursor.getString(cursor.getColumnIndexOrThrow("rentities_code"));

        final String checked = cursor.getString(cursor.getColumnIndexOrThrow("rentities_checked"));

        Log.i("111adapter", "In ImageCursorAdapter: " + entity_title + " = " + checked);

        if (checked.equalsIgnoreCase("1")) {

        Log.i("111adapter", "trying to check...");
        cBox.toggle();

        //cBox.setChecked(true);

        }
        cBox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);

                if (cb.isChecked()) {
                    itemChecked.set(pos, true);
                    itemNames.set(pos, entity_title);
                } 

                else if (!cb.isChecked()) {
                    itemChecked.set(pos, false);
                    itemNames.set(pos, null);
                }

            }
        });
        cBox.setChecked(itemChecked.get(pos));

    }

    public static ArrayList<Boolean> getSetOfCheckedItems() {
        Log.d("111adapter", "itemChecked in ImageCursorAdapter in the end = " + String.valueOf(itemChecked));
        return itemChecked;
    }

    public static ArrayList<String> getSetOfItemsNames() {
        Log.d("111adapter", "itemNames in ImageCursorAdapter in the end = " + String.valueOf(itemNames));
        return itemNames;
    }

但这些方法都没有导致结果:

cBox.toggle();
cBox.setChecked(true);

日志显示checked ==&#39; 1&#39;在某些行中,这是正确的。

09-15 11:14:42.965: I/111adapter(21749): In ImageCursorAdapter: CH01 = 1
09-15 11:14:42.965: I/111adapter(21749): trying to check...

没有任何反应......

2 个答案:

答案 0 :(得分:1)

尽量不要通过findViewById直接定义checkBox 首先以这种方式声明小部件

private class ViewHolder {
    TextView text1;
    TextView text2;
    CheckBox checkBox;
}

其次在newView中定义它们:

@Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        // Inflates the list item layout.
        final View itemLayout =
                mInflater.inflate(R.layout.contact_list_item, viewGroup, false);
        // Creates a new ViewHolder in which to store handles to each view resource. This
        final ViewHolder holder = new ViewHolder();
        holder.text1 = (TextView) itemLayout.findViewById(android.R.id.text1);
        holder.text2 = (TextView) itemLayout.findViewById(android.R.id.text2);
        holder.checkBox = (CheckBox) itemLayout.findViewById(R.id.img);

        // Stores the resourceHolder instance in itemLayout. This makes resourceHolder
        // available to bindView and other methods that receive a handle to the item view.
        itemLayout.setTag(holder);

        // Returns the item layout view
        return itemLayout;
    }

最后在bindView中你可以通过

访问它们
 final ViewHolder holder = (ViewHolder) view.getTag();
  holder.checkBox.setChecked(false);

答案 1 :(得分:-1)

我做

itemChecked.set(pos, checked.equalsIgnoreCase("1"))

而不是

    if (checked.equalsIgnoreCase("1")) {
        Log.i("111adapter", "trying to check...");
        cBox.toggle();
   }

因为最后你使用这个列表来设置检查状态:

cBox.setChecked(itemChecked.get(pos));