Checkbox与SimpleCursorAdapter的任何好例子 [英] Any good example for Checkbox with SimpleCursorAdapter

查看:173
本文介绍了Checkbox与SimpleCursorAdapter的任何好例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 SimpleCursorAdapter ImageView TextView 。它的工作方式是。我试图在我的 listview 中添加 CheckBox ,当我单击 code>我想要从数据库中删除列表行。

I have SimpleCursorAdapter with ImageView and TextView. It works the way it is. I am trying to add CheckBox in my listview, when I click on the checkbox I want the list row to delete from database.

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

Its hard to find example on this case, if you have any example for my situation.

Main。

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

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);


        }


推荐答案

选中此 POST

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

and try to add onCheckChangeListener in your activity or fragment instead of in your adapter

这篇关于Checkbox与SimpleCursorAdapter的任何好例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆