-->

Update a CheckBox in ListView row that corresponds

2020-07-23 06:06发布

问题:

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);
    }
}

回答1:

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
        });

}