【问题标题】:Checkboxes of a ListView are changing theirs values when the List is scrolled滚动列表时,ListView 的复选框正在更改其值
【发布时间】:2023-04-06 13:02:01
【问题描述】:

我正在使用 Android Studio 开发应用程序,但遇到了问题。在我的应用程序中,有一个 ListView 显示使用 arrayAdapter 添加到数据库的主题,并且该列表为您提供选择或不选择主题的选项(带有复选框)。这是带有 ListView 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.cliente.matriadada.AddTurmaActivity">

    <ListView
        android:id="@+id/listaConteudosTurma"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:choiceMode="multipleChoice"
        android:clickable="true"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

</android.support.constraint.ConstraintLayout>

这里是活动代码(Java):

public class AddTurmaActivity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_turma);

        ListView lista = (ListView) findViewById(R.id.listaConteudosTurma);

        MainActivity.BancoController crud = new MainActivity.BancoController(getBaseContext());
        Cursor cursor = crud.carregaConteudos();

        ArrayList<String> valores = new ArrayList<>();

        if (cursor.moveToFirst()){

            do {

                valores.add(cursor.getString(1));

            } while(cursor.moveToNext());

        }

        ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,
                R.layout.linha_com_check_layout, R.id.txtComCheck, valores);

        lista.setAdapter(adaptador);



    }

}

最后是适配器使用的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtComCheck"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBoxLista"
        android:layout_toEndOf="@+id/checkBoxLista"
        android:layout_toRightOf="@+id/checkBoxLista"
        android:text="TextView"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtComCheck" />

</android.support.constraint.ConstraintLayout>

当我启动我的应用并选中一些复选框时,如果我滚动列表,其他复选框也会被选中。我认为这是 RecyclerView 或类似的问题。

我该如何解决这个问题?

【问题讨论】:

  • 这是ListViewCheckBox 的常见问题。请参阅此link 以获得答案

标签: java android listview checkbox android-recyclerview


【解决方案1】:

View Holder 将您的视图保存在一个固定位置,当该位置显示在屏幕上时,它将提取视图。 请点击此链接 Using ViewHolder 很久以前这对我有帮助

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2016-06-03
    • 2018-11-21
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多