我已经设置机器人:可调焦=我在我的自定义布局复选框假
。我的后端SQLite数据库取决于该复选框是否被选中。在我的的ListView
每一行对应一行在我的数据库。所以我的问题是,我应该提上OnClickListener的复选框,以便我可以更新与的ListView
行相关的物品?我需要将它放置在那里我有机会获得一个ID。也许 onListItemClick
?
更新: 在这里,我的自定义适配器:
包com.mohit.geo2do.adapters;
进口java.text.SimpleDateFormat的;
进口的java.util.Calendar;
进口android.content.Context;
进口android.database.Cursor;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.CheckBox;
进口android.widget.CursorAdapter;
进口android.widget.Filterable;
进口android.widget.TextView;
进口com.mohit.geo2do.R;
进口com.mohit.geo2do.provider.Task.Tasks;
进口com.mohit.geo2do.utils.Util;
公共类TasksAdapter扩展的CursorAdapter {
私人最终上下文的背景下;
公共TasksAdapter(上下文的背景下,光标C){
超(背景下,C);
this.context =背景;
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
//充气视图
LayoutInflater充气= LayoutInflater.from(上下文);
视图V = inflater.inflate(R.layout.row_item,父母,假);
返回伏;
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
复选框复选框=(复选框)view.findViewById(R.id.completed);
TextView的DUE_DATE =(TextView中)view.findViewById(R.id.due_date);
字符串标题= cursor.getString(cursor.getColumnIndex(Tasks.TITLE));
布尔完成= Util.intToBool(cursor.getInt(cursor.getColumnIndex(Tasks.COMPLETED)));
SimpleDateFormat的格式=新的SimpleDateFormat(EEEEEE,MMM DD YYYY HH:MM AA);
长unixTime = cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE));
日历由于= Util.timestampToDate(unixTime);
due_date.setText(format.format(due.getTime()));
checkbox.setText(职称);
checkbox.setChecked(已完成);
}
}
解决方案
当我做了类似的事情,我创建了一个SimpleCursorAdapter创造了ViewBinder吧。
在setViewValue()为viewbinder,如果视图是一个instanceof复选框,然后添加一个setOnCheckedChangeListener该更新后端数据库的复选框。如果您需要更多的信息,让我知道。
也许,如果你告诉我你是怎么构建的SimpleCursorAdapter,那么我可以告诉你如何一点走得更远。
公共无效bindView(查看视图,上下文的背景下,光标光标){
复选框复选框=(复选框)view.findViewById(R.id.completed);
TextView的DUE_DATE =(TextView中)view.findViewById(R.id.due_date);
字符串标题= cursor.getString(cursor.getColumnIndex(Tasks.TITLE));
布尔完成= Util.intToBool(cursor.getInt(cursor.getColumnIndex(Tasks.COMPLETED)));
SimpleDateFormat的格式=新的SimpleDateFormat(EEEEEE,MMM DD YYYY HH:MM AA);
长unixTime = cursor.getLong(cursor.getColumnIndex(Tasks.DUE_DATE));
日历由于= Util.timestampToDate(unixTime);
due_date.setText(format.format(due.getTime()));
checkbox.setText(职称);
checkbox.setChecked(已完成);
//编辑在这里..
checkbox.setOnCheckedChangeListener(
新CompoundButton.OnCheckedChangeListener(){
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
//在DB更新你的价值
});
}
I already set android:focusable="false"
on my CheckBox in my custom layout. My back-end SQLite database depends on whether or not the CheckBox is checked or not. Each row in my ListView
corresponds to a row in my database. So my question is, where should I put on OnClickListener for the CheckBox so I can update the item associated with that ListView
row? I would need it to be placed where I have access to an id. Maybe onListItemClick
?
UPDATE: Here my custom adapter:
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);
}
}
解决方案
When I did something similar, I created a SimpleCursorAdapter and created a ViewBinder for it.
In the setViewValue() for the viewbinder, if the view is an instanceof Checkbox, then add a setOnCheckedChangeListener for the checkbox that updates the backend db. If you need more information, let me know.
Maybe if you show me how you constructed the SimpleCursorAdapter, then I can show you how to go a bit further.
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
});
}