Java – Android – checkbox is repeated every 10 times in listview

Hello, I've been trying to create a list view using a custom adapter to place two textviews and a check box in it. The list view works normally, but the check box starts to repeat every 10 seconds, so if you check the first check box, the tenth check box will also check, and so on. I've studied this, but I can't find a specific answer, This answer is well explained, and I can understand what I have to do to solve it. So how do I fix the check boxes so that 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_Check@R_354_2419@"
        android:layout_alignParentTop="true"
        android:height="50dp"/>

    <Check@R_354_2419@
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/workout_items_Check@R_354_2419@"
        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));
    }



}

resolvent:

Try this. I hope it will solve your problem

 public class view_items_CursorAdapter extends CursorAdapter {
 boolean[] check@R_354_2419@;
 public view_items_CursorAdapter(Context context, Cursor cursor, int flags)      
{
    super(context, cursor, 0);
    check@R_354_2419@ = 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);
    Check@R_354_2419@ tvCheck = (Check@R_354_2419@) view.findViewById(R.id.check@R_354_2419@1);

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

    tvBody.setText(body);
    tvPriority.setText(String.valueOf(priority));
    tvCheck.setChecked(check@R_354_2419@[position]);
    tvCheck.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>