【发布时间】:2013-08-16 17:12:15
【问题描述】:
我正在使用自定义列表适配器来显示带有复选框、文本和日期的列表。
[复选框] - [TextView] - [日期]
我正在从数据库中填充列表,但现在我想要的是,如果我检查一个列表项,那么它已完成并从列表视图中淡出,数据库中的状态也应该使用更新集查询为真,但我不想删除它来自数据库。如何设置未选中项目的列表。
我的自定义适配器:
public class CustomAdapter extends ArrayAdapter<Task> {
private List<Task> dataitem;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId, List<Task> items) {
super(a, textViewResourceId, items);
this.dataitem = items;
this.activity = a;
}
public static class ViewHolder{
public TextView tasklistTitle;
public TextView createdDate;
public CheckBox completedflag;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.tasklist_row, null);
holder = new ViewHolder();
holder.tasklistTitle = (TextView) v.findViewById(R.id.tasklistTitle);
holder.createdDate = (TextView) v.findViewById(R.id.createdDate);
holder.completedflag = (CheckBox) v.findViewById(R.id.completedflag);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Task custom = dataitem.get(position);
if (custom != null) {
holder.tasklistTitle.setText(custom.getTaskListTitle());
holder.createdDate.setText(custom.getTaskListCreated());
holder.completedflag.setText(custom.getTaskListCompletedFlag());
}
return v;
}
public synchronized void refresAdapter(List<Task> dataitems) {
dataitem.clear();
dataitem.addAll(dataitems);
notifyDataSetChanged();
}
}
在这个类中是否需要使用 OncheckedCheckedListner?我已经经历了几个例子,但没有用。请帮忙。谢谢
【问题讨论】:
-
我真的不明白你的问题。这是您的问题吗:1.当前选中一个复选框 2.选中一个复选框时,您希望将选中状态更新到数据库(当然包括更改先前选中的状态)
-
是的,没错,但我不知道如何获得未经检查的列表。请推荐?
-
@hieuxit 有什么进一步的评论吗?
-
好的! 0. 声明一个 int mStoreCheckedPosition = -1; // 保存当前选中的位置 1. 你 setTag for Checkbox 在每个项目上是它的位置(在 getView() 函数中, holder.completedflag.setTag(position)) 2. 你使用 OncheckedChecngeListner 来确定选中的复选框(checked = = true) 并检查条件 if(mStoreCheckedPosition != -1){ // updateView int oldCheckedPosition = mStoreCheckedPosition - listView.getFirstVisiblePosition();查看孩子 = listView.getChildAt(oldCheckedPosition); // 获取 holder 和 setChecked for checked 为 false }(继续...)
-
抱歉评论是很少的字符,我不能完整地发布,我会在答案中发布完整的。
标签: android android-listview android-adapter