gpt4 book ai didi

java - 更新对应于数据库行的 ListView 行中的 CheckBox

转载 作者:太空狗 更新时间:2023-10-29 12:57:35 24 4
gpt4 key购买 nike

我已经在自定义布局的 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() 中,如果 View 是 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com