简体   繁体   中英

Problem with customizable adapter in RecycleView - Android/Java

I'm implementing a Customizable Adapter with TextView, EditText and CheckBox. I managed to make all the items appear in my RecycleView, however when checking a checkbox, for example, another checkbox for an item below is also checked, the same occurs with editing EditText, when changing the value of an item one another one on the list is also changed by the same value. I don't know how I can avoid this situation, since I performed the seemingly correct implementation of the viewholder, follow the code below the implementation:

RecycleView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns: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=".ActivityListaDisciplinaDiario">

<android.support.v7.widget.RecyclerView
    android:id="@+id/lista_bncc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

Layout Adapter:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textStyle="bold"
        android:text="Código BNCC:"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txt_bncc"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="18sp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="TextView" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textStyle="bold"
        android:text="Descrição:"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txt_descricao_bncc"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="18sp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="TextView" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textStyle="bold"
        android:text="Tempo:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/timer"
        android:inputType="number"
        android:hint="tempo de aula"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end">

        <CheckBox
            android:layout_gravity="end"
            android:id="@+id/checkbox_bncc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

Activity Code:

lista = response.body();

            Adapter_Lista_Bncc adapter = new Adapter_Lista_Bncc(lista, ActivityListaBNCC.this);

            lista_bncc.setAdapter(adapter);
            RecyclerView.LayoutManager layout = new LinearLayoutManager(ActivityListaBNCC.this, 
            LinearLayoutManager.VERTICAL, false);

            lista_bncc.setLayoutManager(layout);

Adapter Implementation Code:

public class Adapter_Lista_Bncc extends RecyclerView.Adapter {

private List<Pesquisa> lista;

private Context context;

public Adapter_Lista_Bncc(List<Pesquisa> lista, Context context){

    this.lista = lista;
    this.context = context;

}


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(context).inflate(R.layout.adapter_lista_bncc, viewGroup, false);

    NossoViewHolder holder = new NossoViewHolder(view);


    return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    NossoViewHolder holder = (NossoViewHolder) viewHolder;

    Pesquisa p = lista.get(i);

    holder.txt_bncc.setText(p.getVar1());
    holder.txt_descricao_bncc.setText(p.getVar2());
    holder.timer.setFilters(new InputFilter[]{new MinMaxFilter("1", "60")});

}

@Override
public int getItemCount() {
    return lista.size();
}

public class NossoViewHolder extends RecyclerView.ViewHolder{

    final TextView txt_bncc;
    final TextView txt_descricao_bncc;
    final EditText timer;
    final CheckBox check;

    public NossoViewHolder(View v){
        super(v);

        txt_bncc = (TextView) v.findViewById(R.id.txt_bncc);
        txt_descricao_bncc = (TextView) v.findViewById(R.id.txt_descricao_bncc);
        timer = (EditText) v.findViewById(R.id.timer);
        check = (CheckBox) v.findViewById(R.id.checkbox_bncc);

    }

}
}

I am available to provide more information about the situation, I thank you for your attention and anyone who can help.

Okay first of all fo better performance migrate to Androidx and implement on click listner and on your adapter you should have to implement interface to pass your position so that you can access in your activity and change them i will rectify your adapter like below for check box only check for others

public class Adapter_Lista_Bncc extends RecyclerView.Adapter {

private List<Pesquisa> lista;

private Context context;
// here is our interface 
private OnItemClickListner listner;

public Adapter_Lista_Bncc(List<Pesquisa> lista, Context context){

    this.lista = lista;
    this.context = context;

}


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(context).inflate(R.layout.adapter_lista_bncc, viewGroup, false);

    NossoViewHolder holder = new NossoViewHolder(view);


    return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    NossoViewHolder holder = (NossoViewHolder) viewHolder;

    Pesquisa p = lista.get(i);

    holder.txt_bncc.setText(p.getVar1());
    holder.txt_descricao_bncc.setText(p.getVar2());
    holder.timer.setFilters(new InputFilter[]{new MinMaxFilter("1", "60")});

}

@Override
public int getItemCount() {
    return lista.size();
}
// implemet on click listner
public class NossoViewHolder extends RecyclerView.ViewHolder implements View.OnClickLisntener{

    final TextView txt_bncc;
    final TextView txt_descricao_bncc;
    final EditText timer;
    final CheckBox check;

    public NossoViewHolder(View v){
        super(v);

        txt_bncc = (TextView) v.findViewById(R.id.txt_bncc);
        txt_descricao_bncc = (TextView) v.findViewById(R.id.txt_descricao_bncc);
        timer = (EditText) v.findViewById(R.id.timer);
        check = (CheckBox) v.findViewById(R.id.checkbox_bncc);
       check.setOnClickListener(this);


    }
@Override
public void onClick(View view)
{
// you should have to pass your position i.e what position what your clicking
int position = getAdapterPosition();
// to avoid erro if recyclerView is empty use rvNo position
if(listner!=null && position!= RecyclerView.No_Position)
{
listner.onClick(lista.get(position),view);
}

}
}

// here is Interface
public interface OnItemClickListner{
void onClick(Pesquisa xyz,View view);
}
public void setOnItemClicklistner(// interface OnItemClickListner lis)
{
this.listner = lis;
}
}

now on your activity call your interface

adapter.setOnItemClicklistner(new //your adapter. Onclick Listner) 
{
@override 
public void onClick(...auto generated)
{
// do your stuff using instance of check box
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM