我正在使用自定义列表适配器来显示包含复选框,文本和日期的列表。
[复选框] - [文本视图] - [日期]
我正在从数据库填充列表但现在我想要的是,如果我检查一个列表项然后它已完成并从列表视图中淡出,数据库中的状态应该是真的使用更新集查询但我不想删除它来自数据库。如何使用未选中的项目设置列表。
我的自定义适配器:
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();
}
}
是否需要在此课程中使用OncheckedChecngeListner?我已经完成了几个例子,但没有用。请帮忙。谢谢
答案 0 :(得分:0)
您使用OncheckedChecngeListner来确定选中的复选框: if(选中== true)。 并按照指南:
if(mStoreCheckedPosition != -1){
// updateView
int oldCheckedPosition = mStoreCheckedPosition - listView.getFirstVisiblePosition();
View child = listView.getChildAt(oldCheckedPosition); // Get holder and setChecked for checked is false
if(child!=null){
((ViewHolder)child.getTag()).completedflag.setChecked(false);
}
}
// update database
int currentCheckedPosition = (Integer)view.getTag();
// Update database with the currentCheckedPosition is true value and mStoreCheckedPosition is false value
// finally update mStoreCheckedPosition
mStoreCheckedPosition = currentCheckedPosition;
我建议您在列表适配器中为getItemId返回databaseId,然后使用它来更新数据库