任何使用SimpleCursorAdapter的Checkbox的好例子

时间:2014-02-03 04:50:47

标签: android listview checkbox android-listview

SimpleCursorAdapterImageView以及TextView。它的工作方式就是这样。我正在尝试在CheckBox中添加listview,当我点击checkbox我希望列表行从数据库中删除时。

如果你对我的情况有任何例子,很难在这个案例中找到例子。

Main.class

public void forGeneral(){
        c = m.fetchAllReminders();

        String[] from = new String[]{Database.KEY_FIRST, Database.KEY_LAST};
        int[] to = new int[]{R.id.tFirst, R.id.tSecond};
        r2 = new ImageV(getActivity().getApplicationContext(), R.layout.forcheckbox, c, from, to);
        h = r2.getItemId(c.getPosition());

        r2.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View arg0, Cursor arg1, final int arg2) {
                // TODO Auto-generated method stub
                if(arg0.getId() == R.id.cbCheckk){

                    final CheckBox cb1 = (CheckBox) arg0.findViewById(R.id.cbCheckk) ;
                    cb1.setTag(arg2);
                    cb1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub

                        }

                    });



                }
                return false;
            }
        });

        setListAdapter(r2);



    }

ImageV.class

public class ImageV extends SimpleCursorAdapter{

    private Cursor cn;
    private Context context;
    private ViewHolder holder = null;
    private Layout mlayout;
    private ArrayList<String> list = new ArrayList<String>();
    private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
    private int i;

    public ImageV(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.cn= c;
        this.context = context;

        for(int i = 0; i < this.getCount(); i++){
            itemChecked.add(i, false);
        }


    }
    private class ViewHolder{
        TextView mFirst;
        TextView mLast;
        CheckBox mCheckBox;
        ListView lv;

    }

    public View getView (final int pos, View inView, ViewGroup parent){


        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if(inView == null){
            inView = mInflater.inflate(R.layout.forcheckbox, null);
            holder = new ViewHolder();
            holder.mFirst = (TextView) inView.findViewById(R.id.tFirst);
            holder.mLast = (TextView) inView.findViewById(R.id.tSecond);
            holder.mCheckBox = (CheckBox) inView.findViewById(R.id.cbCheckk);
            inView.setTag(holder);
        }else {
            holder = (ViewHolder) inView.getTag();

        }
        this.cn.moveToPosition(pos);
        String first = c.getString(c.getColumnIndex(Database.KEY_FIRST));
        String last = c.getString(c.getColumnIndex(Database.KEY_LAST));


        holder.mFirst.setText(first);
        holder.mLast.setText(last);
        holder.mCheckBox.setTag(pos);
        holder.mCheckBox.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v.findViewById(R.id.cbCheckk);
                if(cb.isChecked()){

                    itemChecked.set(pos, true);
                    Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_LONG).show();
                    long u = c.getLong(i);
                    m.deleteReminder(u);
                    cb.setChecked(false);
                    c.requery();
                }


            }

        });

        return (inView);


        }

2 个答案:

答案 0 :(得分:0)

好的,以下是你的答案。

<强> yourName.XML

<CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/classname"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/classImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp" />

<强> CustomAdapter

public class Custom_ClassAdapter extends BaseAdapter {

    LayoutInflater inflater;
    Context context;

    List<List_ClassModel> rowItems;

    public Custom_ClassAdapter(Context context, List<List_ClassModel> rowItems) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.rowItems = rowItems;
    }

    private class ViewHolder {
        ImageView mImageView;
        TextView mTextView;
        CheckBox mCheckBox;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_class, null);
            holder = new ViewHolder();
            holder.mImageView = (ImageView) convertView
                    .findViewById(R.id.classImage);
            holder.mTextView = (TextView) convertView
                    .findViewById(R.id.classname);
            holder.mCheckBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mImageView.setImageResource(position);

        holder.mCheckBox.setTag(position);
        holder.mCheckBox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        // TODO Auto-generated method stub
                         Code.classDel = (Integer) buttonView.getTag();
                    }
                });

Code.classDel是一个静态整数变量。

答案 1 :(得分:0)

选中此POST

并尝试在您的活动或片段中添加onCheckChangeListener,而不是在您的适配器中添加