带有CursorAdapter的ListView中的复选框监听器

时间:2014-05-19 19:45:32

标签: android listview checkbox

在我的ListView的每一行中,我都有一个带有监听器的复选框。监听器更新数据库行。

MyAdapter类是:

public class MyAdapter extends CursorAdapter {

Context b;  
LayoutInflater inflater;
@SuppressWarnings("deprecation")
public MyAdapter(Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
    b= (Context) context;

}

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

    TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
    TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);

    tv1.setText(cursor.getString(2));
    tv2.setText(cursor.getString(3));

    final int pos = cursor.getPosition();

    final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);

    String likes = cursor.getString(cursor.getColumnIndex("like"));
    if (likes.equals("yes")) {
        repeatChkBx.setChecked(true);

    } else {
        repeatChkBx.setChecked(false);
    }

    repeatChkBx.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            MyDatabase mydatabase = new MyDatabase(b);
            SQLiteDatabase mydb = mydatabase.getWritableDatabase();
            cursor.moveToPosition(pos);

            if (repeatChkBx.isChecked()) {
                ContentValues cv = new ContentValues();
                cv.put("like", "yes");
                mydb.update("list", cv, "id ="+cursor.getString(1), null);


            } else {
                ContentValues cv = new ContentValues();
                cv.put("like", "no");
                mydb.update("list", cv, "id ="+cursor.getString(1), null);
            }

            mydb.close();
        }
    });

    }

    protected Context getActivity() {
        // TODO Auto-generated method stub
        return null;
    }

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

    }
}

我有这个问题,移动列表我丢失了从屏幕上消失的行的复选框状态。 像这张照片:

enter image description here

1 个答案:

答案 0 :(得分:0)

检查以下代码,我已对您的代码进行了一些更改,...希望它能解决您的问题

public class MyAdapter extends CursorAdapter {
   Context b;  
   LayoutInflater inflater;
   ArrayLis<Boolean> checkbox_arr = new ArrayLis<Boolean>();
   @SuppressWarnings("deprecation")
   public MyAdapter(Context context, Cursor c) {
      super(context, c);
      for(int i=0;i<c.getCount();i++){
         checkbox_arr.add(false); 
      }  
   inflater = LayoutInflater.from(context);
   b= (Context) context;

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

TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);

tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));

final int pos = cursor.getPosition();

final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);

String likes = cursor.getString(cursor.getColumnIndex("like"));

if (likes.equals("yes")) {
    checkbox_arr.set(pos,true);


} else {
     checkbox_arr.set(pos,false);

}
repeatChkBx.setChecked(checkbox_arr.get(pos));
repeatChkBx.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        MyDatabase mydatabase = new MyDatabase(b);
        SQLiteDatabase mydb = mydatabase.getWritableDatabase();
        cursor.moveToPosition(pos);

        if (repeatChkBx.isChecked()) {
             checkbox_arr.set(pos,false);   //
             ContentValues cv = new ContentValues();
            cv.put("like", "no");
            mydb.update("list", cv, "id ="+cursor.getString(1), null);


        } else {
             checkbox_arr.set(pos,true); //

            ContentValues cv = new ContentValues();
            cv.put("like", "yes");  //
            mydb.update("list", cv, "id ="+cursor.getString(1), null);
        }
        repeatChkBx.setChecked(checkbox_arr.get(pos));  //
        notifyDataSetChanged();
        mydb.close();
    }
});

}

protected Context getActivity() {
    // TODO Auto-generated method stub
    return null;
}

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

 }
}