i have the problem about when user click the checkbox
inside the listView
, the textView
will no charge it to Paint.STRIKE_THRU_TEXT_FLAG)
. When the user click the checkbox
,the strike line will display in the textView
.
public void bindView(View row, Context context, Cursor c) {
// TODO Auto-generated method stub
listName = (TextView) row.findViewById(R.id.produtName);
listCheck = (CheckBox) row.findViewById(R.id.check);
Item tag = (Item) listCheck.getTag();
String pos = helper.getProductId(c);
Log.i(CN, "getView: no tag on " + pos);
tag = new Item();
tag.id = Integer.parseInt(pos);
listCheck.setTag(tag);
listCheck.setChecked(false);
String status = helper.getProductStatusT(c);
if (Integer.parseInt(status) == 0) {
listCheck.setChecked(true);
listName.setPaintFlags(listName.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
} else {
listCheck.setChecked(false);
listName.setPaintFlags(listName.getPaintFlags()
& (~Paint.STRIKE_THRU_TEXT_FLAG));
}
public View getView(int position, View convertView, ViewGroup parent) {
View tmpView = super.getView(position, convertView, parent);
Log.i(CN, "getView:" + position);
final CheckBox cBox = (CheckBox) tmpView.findViewById(R.id.check);
Item tag = (Item) cBox.getTag();
cBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Item tag = (Item) v.getTag();
if (tag == null)
Log.i(CN, "checkbox clicked no tag");
else
helper.updateStatus(tag.id);
Log.i(CN, "checkbox clicked tag=" + tag.id);
}
if (cBox.isChecked()) {
Log.i(CN, " Checked!");
// do some operations here
} else {
Log.i(CN, "NOT Checked!");
// do some operations here
helper.updateStatus2(tag.id);
}
}
});
return tmpView;
}
the checkBox work fine . But the problem is when user click the checkbox , the strike line will not display out . Anyone know where should i put the
listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
I think i might have an idea what you might be trying now, see if the following is working for you:
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater inflator;
private boolean[] arrCb;
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arrCb = new boolean[c.getCount()];
// ^ this will hold the checkbox states
resetArrcb();
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
View v = inflator.inflate(R.layout.lv_row, parent, false);
// inflate the xml that the textView and checkbox is in
return v;
}
@Override
public void bindView(View row, Context context, final Cursor cursor) {
// you do everything else here. there's no need for getView when you use newView and bindView
final TextView listName = (TextView) row.findViewById(R.id.produtName);
final CheckBox listCheck = (CheckBox) row.findViewById(R.id.check);
if (arrCb[cursor.getPosition()]) {
listCheck.setChecked(true);
listName.setPaintFlags(listName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
listCheck.setChecked(false);
listName.setPaintFlags(listName.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
listCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listCheck.isChecked()) {
arrCb[cursor.getPosition()] = true;
} else {
arrCb[cursor.getPosition()] = false;
}
notifyDataSetChanged();
}
});
}
private void resetArrcb() {
// i'm using this to fill the collection but you could easily loop through your
// database to fill it with the correct values.
for (int i = 0; i < arrCb.length; i++) {
arrCb[i] = false;
}
}
}
Then i'm not sure, but it seems like you'd want to adjust your database depending on what is selected. I'd suggest doing it when everything is all over, when your activity is being exited. perhaps set a new method that will just loop through the checkbox collection then do what you intended in the database. it should match 1 to 1 with with table rows.
EDIT
Even even better, make a getter method to return your array instead so you can do your database operations right in your activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With