lundi 15 janvier 2018

Checkboxes of a ListView are changing theirs values when the List is scrolled

I'm using Android Studio to develop an application and i'm having a problem. In my app, there is a ListView that shows subjects added to a database using an arrayAdapter and the list gives you the option to select or not a subject (with checkboxes). Here is the XML file with the ListView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    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>

Here is the Activity code (in 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);



    }

}

And finally the XML file that the adaptor uses:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    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>

When I launch my app and check some checkboxes, if I scroll the list other checkboxes get checked too. I think it's a problem with RecyclerView or something like this.

How can I fix this?




Aucun commentaire:

Enregistrer un commentaire