java-更新与数据库行相对应的ListView行中的CheckBox
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-更新与数据库行相对应的ListView行中的CheckBox,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3051字,纯文字阅读大概需要5分钟。
内容图文
我已经在自定义布局的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的实例,则为该复选框添加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
});
}
内容总结
以上是互联网集市为您收集整理的java-更新与数据库行相对应的ListView行中的CheckBox全部内容,希望文章能够帮你解决java-更新与数据库行相对应的ListView行中的CheckBox所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。