我有一个问题,当用户点击checkbox里面的时候listView,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;
}
Run Code Online (Sandbox Code Playgroud)
复选框工作正常。但问题是当用户单击复选框时,罢工线不会显示出来。有谁知道我应该把它放在哪里
listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)
我想我可能知道您现在可能正在尝试什么,看看以下内容是否适合您:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么我不确定,但似乎您想根据选择的内容调整数据库。我建议在一切都结束、您的活动即将退出时执行此操作。也许设置一个新方法,该方法将循环遍历复选框集合,然后在数据库中执行您想要的操作。它应该与表行 1 对 1 匹配。
编辑
更好的是,创建一个 getter 方法来返回数组,这样您就可以在活动中直接执行数据库操作。
| 归档时间: |
|
| 查看次数: |
5006 次 |
| 最近记录: |