Android - Checkbox repeated every 10 in the list

Hello, I am trying to create a listview using a custom adapter to put two text fields and one checkbox in it. Listviews work fine, but the checkboxes start repeating every 10, so if you check the first checkbox, the 10th check also and so on. I researched this, but could not find a specific answer that was well explained that I could understand exactly what I had to do to fix it. So, how do I set the checkboxes so they don't repeat?

act_view_items_view.xml   

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/workout_items_textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:gravity="center_vertical"
        android:layout_toStartOf="@+id/workout_items_textView2"
        android:layout_toLeftOf="@+id/workout_items_textView2"
        android:textIsSelectable="false"
        android:height="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/workout_items_textView2"
        android:textSize="20dp"
        android:gravity="center_vertical"
        android:layout_toLeftOf="@+id/workout_items_CheckBox"
        android:layout_alignParentTop="true"
        android:height="50dp"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/workout_items_CheckBox"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:height="40dp"
        android:layout_alignBottom="@+id/workout_items_textView2"
        android:focusable="false"/>

</RelativeLayout>

view_items.java

public class view_items extends ListActivity {
    private ListAdapter listAdapter;
    private ListAdapter listAdapter2;
    private TaskDBHelper helper;
    private TextView taskTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_view_items);

        updateUI2();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Add a Workout");
                builder.setMessage("Enter the name of the workout");
                final EditText inputField = new EditText(this);
                builder.setView(inputField);
                builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        AlertDialog.Builder builder2 = new AlertDialog.Builder(view_items.this);
                        builder2.setTitle("Add a Workout");
                        builder2.setMessage("Enter the reps for the workout");
                        final EditText inputField2 = new EditText(view_items.this);
                        builder2.setView(inputField2);
                        builder2.setPositiveButton("Next", new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String task = inputField.getText().toString();
                                String reps = inputField2.getText().toString();
                                String workoutID = TaskContract.UNSTABLE_WORKOUT_ID;

                                helper = new TaskDBHelper(view_items.this);
                                SQLiteDatabase db = helper.getWritableDatabase();
                                ContentValues values = new ContentValues();

                                values.clear();
                                values.put(TaskContract.Columns.TASK, task);
                                values.put(TaskContract.Columns.REPS, reps);
                                values.put(TaskContract.Columns.WORKOUT_ID, workoutID);

                                db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
                                updateUI2();
                            }


                        });
                        builder2.setNegativeButton("Cancel", null);

                        builder2.create().show();

                    }


                });

                builder.setNegativeButton("Cancel",null);

                builder.create().show();
                return true;

            default:
                return false;
        }
    }


    public void updateUI2(){

        // TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite
        TaskDBHelper handler = new TaskDBHelper(this);
        // Get access to the underlying writeable database
        SQLiteDatabase db = handler.getWritableDatabase();
        // Query for items from the database and get a cursor back
        String sql2 = String.format("SELECT  * FROM workout_items WHERE %s = '%s'",
                TaskContract.Columns.WORKOUT_ID,
                TaskContract.UNSTABLE_WORKOUT_ID);


        Cursor todoCursor = db.rawQuery(sql2, null);

        // Find ListView to populate
        setContentView(R.layout.act_view_items);
        ListView lvItems = (ListView) findViewById(android.R.id.list);
        // Setup cursor adapter using cursor from last step
        view_items_CursorAdapter todoAdapter = new view_items_CursorAdapter(this, todoCursor, 0);
        // Attach cursor adapter to the ListView
        lvItems.setAdapter(todoAdapter);




    }

    public void onDoneButtonClick(View view) {
        View v = (View) view.getParent();
        TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView);
        String task = taskTextView.getText().toString();

        String sql = String.format("DELETE FROM %s WHERE %s = '%s'",
                TaskContract.TABLE,
                TaskContract.Columns.TASK,
                task);


        helper = new TaskDBHelper(view_items.this);
        SQLiteDatabase sqlDB = helper.getWritableDatabase();
        sqlDB.execSQL(sql);
        updateUI2();
    }

}

view_items_CursorAdapter.java

public class view_items_CursorAdapter extends CursorAdapter {

    public view_items_CursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, 0);
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.act_view_items_view, parent, false);
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {


        TextView tvBody = (TextView) view.findViewById(R.id.workout_items_textView);
        TextView tvPriority = (TextView) view.findViewById(R.id.workout_items_textView2);

        String body = cursor.getString(cursor.getColumnIndexOrThrow("task"));
        int priority = cursor.getInt(cursor.getColumnIndexOrThrow("reps"));

        tvBody.setText(body);
        tvPriority.setText(String.valueOf(priority));
    }



}
+4
source share
1 answer

. , .

 public class view_items_CursorAdapter extends CursorAdapter {
 boolean[] checkbox;
 public view_items_CursorAdapter(Context context, Cursor cursor, int flags)      
{
    super(context, cursor, 0);
    checkbox = new boolean[cursor.getCount()];
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.act_view_items_view, parent, false);
}


@Override
public void bindView(View view, Context context, Cursor cursor) {

    final int position = cursor.getPosition();
    TextView tvBody = (TextView) view.findViewById(R.id.workout_items_textView);
    TextView tvPriority = (TextView) view.findViewById(R.id.workout_items_textView2);
    CheckBox tvCheck = (CheckBox) view.findViewById(R.id.checkbox1);

    String body = cursor.getString(cursor.getColumnIndexOrThrow("task"));
    int priority = cursor.getInt(cursor.getColumnIndexOrThrow("reps"));

    tvBody.setText(body);
    tvPriority.setText(String.valueOf(priority));
    tvCheck.setChecked(checkbox[position]);
    tvCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         checkbox[position] = isChecked;
       }});
}
0

Source: https://habr.com/ru/post/1608805/


All Articles