Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - Android RecyclerAdapter onBindViewHolder can't acess ui elements

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 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);
    }
}

Why don't you change this like to this one. ↓

     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);
        name = view.findViewById(R.id.exerciseName1);
        reps = view.findViewById(R.id.reps1);
        sets = view.findViewById(R.id.sets1);
        weight = view.findViewById(R.id.weigh1);
        System.out.println(name);
    }
}

I think you don't have to declare the variables one more time. If you declare the variable inside the MyViewHolder scope, it can't be checked from the outside. Please try this way and if you can't solve the problem, please write a comment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...