我遇到的问题是,当用户单击 listView
中的 checkbox
时,textView
不会向 Paint 收费。 STRIKE_THRU_TEXT_FLAG)
。当用户单击 checkbox
时,删除线将显示在 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;
}
复选框工作正常。但问题是当用户点击复选框时,删除线不会显示出来。任何人都知道我应该把
listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
最佳答案
我想我可能知道您现在正在尝试什么,看看以下是否适合您:
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;
}
}
}
那么我不确定,但您似乎想要根据所选内容调整数据库。我建议在一切都结束时,在您的 Activity 退出时执行此操作。也许设置一个新方法,它将只循环遍历复选框集合,然后在数据库中执行您想要的操作。它应该与表行一对一匹配。
编辑
更好的是,创建一个 getter 方法来返回数组,这样您就可以在 Activity 中直接执行数据库操作。
关于android - 在 textView 中设置 Paint.STRIKE_THRU_TEXT_FLAG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658052/