mardi 21 avril 2015

Update database when checkbox in listview change - android

i storage checkbox in database by a field. This field return 0 or 1 (mean: false or true). This is my code in custom adapter:

public class CauHoiAdapter extends ArrayAdapter<CauHoiObject> {
    Context mContext;
    ArrayList<CauHoiObject> arlCauHoi = new ArrayList<CauHoiObject>();
    String cauHoi = null;

    CauHoiDAO cauhoidao = new CauHoiDAO(getContext());

    public CauHoiAdapter(Context context, int resource,
            ArrayList<CauHoiObject> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        this.mContext = context;
        this.arlCauHoi = new ArrayList<CauHoiObject>(objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.item_cauhoi, parent, false);

        ImageView imgCauHoi = (ImageView) rowView
                .findViewById(R.id.img_cauhoi_icon);
        TextView txtCauHoi = (TextView) rowView.findViewById(R.id.textCauHoi);
        CheckBox chkFavorate = (CheckBox) rowView
                .findViewById(R.id.star_favorite);
        cauHoi = arlCauHoi.get(position).getCauHoi();
        imgCauHoi.setImageResource(arlCauHoi.get(position).getIconCauHoi());
        txtCauHoi.setText(arlCauHoi.get(position).getCauHoi());
        chkFavorate.setChecked(arlCauHoi.get(position).isFavorate());

        chkFavorate.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    cauhoidao.updateYeuThich(cauHoi, 1);
                }
                else{
                    cauhoidao.updateYeuThich(cauHoi, 0);
                } 

            }
        });
        return rowView;
    }

How can I change database when checkbox click????

Sorry because my english, but help me plz...




Aucun commentaire:

Enregistrer un commentaire