I am trying to make a Recycler view with multiple EditText
elements in a single row. The recycler adapter can make the elements but when I try to set their text I get an error.
I did some debugging and turns out that the onBindViewHolder()
gets those EditText
elements but they are null
.
I can't figure out what to do since they are loaded correctly because when I print them in the constructor they are not null
so It has nothing to with the Ids.
Here is the code:
package com.example.recyclerviewtest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<Exercise> exerciseList;
public RecyclerAdapter(ArrayList<Exercise> exerciseList) {
this.exerciseList = exerciseList;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public EditText name;
public EditText reps;
public EditText sets;
public EditText weight;
public MyViewHolder(final View view) {
super(view);
EditText name = view.findViewById(R.id.exerciseName1);
EditText reps = view.findViewById(R.id.reps1);
EditText sets = view.findViewById(R.id.sets1);
EditText weight = view.findViewById(R.id.weigh1);
System.out.println(name);
}
}
@NonNull
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.MyViewHolder holder, int position) {
String Name = exerciseList.get(position).name;
String Reps = String.valueOf(exerciseList.get(position).reps);
String Sets = String.valueOf(exerciseList.get(position).sets);
String Weight = String.valueOf(exerciseList.get(position).weight);
// holder.name.setText(Name);
// holder.reps.setText(Reps);
// holder.sets.setText(Sets)
// holder.weight.setText(Weight);
}
@Override
public int getItemCount() {
return exerciseList.size();
}
}
and here is the xml for the single row:
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/exerciseName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:hint="Exercise name"
android:textSize="18sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="72dp" />
<EditText
android:id="@+id/sets1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/reps1"
android:hint="Sets"
android:inputType="numberSigned"
android:paddingRight="16dp"
android:textSize="18sp"
tools:layout_editor_absoluteX="214dp"
tools:layout_editor_absoluteY="72dp" />
<EditText
android:id="@+id/reps1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/linear1"
android:hint="Reps"
android:inputType="numberSigned"
android:paddingRight="16dp"
android:textSize="18sp"
tools:layout_editor_absoluteX="274dp"
tools:layout_editor_absoluteY="72dp" />
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:minWidth="59dp"
android:orientation="horizontal">
<EditText
android:id="@+id/weigh1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:maxWidth="250dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/value_unit_symbol"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:text="kg"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
question from:
https://stackoverflow.com/questions/65649270/android-recycleradapter-onbindviewholder-cant-acess-ui-elements 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…