[英]Update a CheckBox in ListView row that corresponds to a database row
我已经在自定义布局中的CheckBox上设置了android:focusable="false"
。 我的后端SQLite数据库取决于CheckBox是否被选中。 ListView
每一行都对应于我的数据库中的一行。 所以我的问题是,我应该在哪里为CheckBox放置OnClickListener,以便我可以更新与该ListView
行关联的项目? 我需要将它放在我可以访问id的位置。 也许onListItemClick
?
更新:
这是我的定制适配器:
package com.mohit.geo2do.adapters;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.TextView;
import com.mohit.geo2do.R;
import com.mohit.geo2do.provider.Task.Tasks;
import com.mohit.geo2do.utils.Util;
public class TasksAdapter extends CursorAdapter {
private final Context context;
public TasksAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Inflate the view
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.row_item, parent, false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
CheckBox checkbox = (CheckBox)view.findViewById(R.id.completed);
TextView due_date = (TextView)view.findViewById(R.id.due_date);
String title = cursor.getString(cursor.getColumnIndex(Tasks.TITLE));
boolean completed = Util.intToBool(cursor.getInt(cursor.getColumnIndex(Tasks.COMPLETED)));
SimpleDateFormat format = new SimpleDateFormat("EEEEEE, MMM dd yyyy hh:mm aa");
long unixTime = cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE));
Calendar due = Util.timestampToDate(unixTime);
due_date.setText(format.format(due.getTime()));
checkbox.setText(title);
checkbox.setChecked(completed);
}
}
当我做类似的事情时,我创建了一个SimpleCursorAdapter并为它创建了一个ViewBinder。
在viewbinder的setViewValue()中,如果视图是Checkbox的实例,则为更新后端db的复选框添加setOnCheckedChangeListener。 如果您需要更多信息,请告诉我们。
也许如果你告诉我你是如何构建SimpleCursorAdapter的话,那么我可以告诉你如何更进一步。
public void bindView(View view, Context context, Cursor cursor) {
CheckBox checkbox = (CheckBox)view.findViewById(R.id.completed);
TextView due_date = (TextView)view.findViewById(R.id.due_date);
String title = cursor.getString(cursor.getColumnIndex(Tasks.TITLE));
boolean completed = Util.intToBool(cursor.getInt(cursor.getColumnIndex(Tasks.COMPLETED)));
SimpleDateFormat format = new SimpleDateFormat("EEEEEE, MMM dd yyyy hh:mm aa");
long unixTime = cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE));
Calendar due = Util.timestampToDate(unixTime);
due_date.setText(format.format(due.getTime()));
checkbox.setText(title);
checkbox.setChecked(completed);
// edit here ..
checkbox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// update your value in the db
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.