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

java-更新与数据库行相对应的ListView行中的CheckBox,第1张

概述我已经在自定义布局的CheckBox上设置了android:focusable=“false”.我的后端SQLite数据库取决于是否选中了CheckBox.ListView中的每一行都对应于数据库中的一行.所以我的问题是,我应该在哪里放置CheckBox的OnClickListener,以便可以更新与该ListView行关联的项目?我需要将其放置

我已经在自定义布局的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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1068474.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存