有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更新ListView行中与数据库行对应的复选框

我已经在自定义布局中的复选框上设置了安卓:focusable="false"。我的后端SQLite数据库取决于复选框是否选中。myListView中的每一行对应于我的数据库中的一行。所以我的问题是,我应该在哪里为复选框设置OnClickListener,以便更新与ListView行关联的项?我需要把它放在我可以访问id的地方。也许onListItemClick

更新:
这里是我的自定义适配器:

package com.mohit.geo2do.adapters;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import 安卓.content.Context;
import 安卓.database.Cursor;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.CheckBox;
import 安卓.widget.CursorAdapter;
import 安卓.widget.Filterable;
import 安卓.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);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    当我做类似的事情时,我创建了一个SimpleCorsorAdapter并为其创建了一个ViewBinder

    在viewbinder的setViewValue()中,如果视图是instanceof复选框,则为更新后端数据库的复选框添加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
            });
    

    }