Search code examples
javatextview

in RecyclerView, in OnBindViewHolder method


In RecyclerView, the OnBindViewHolder method , I can not get the TextViews that are located in the ViewHolder class. Why? What is the problem?

Please refer to the screenshot: enter image description here

And my code below:

public class MainActivity extends AppCompatActivity  {

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

        ((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout)).setTitle("Screen Title");

        RecyclerView rv = findViewById(R.id.recyclerview);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            @Override
            public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int position) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                return new ViewHolder(view);}

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder  viewHolder, int position) {
                viewHolder.text1.setText("Bacon");
                viewHolder.text2.setText("Bacon ipsum dolor amet pork belly meatball kevin spare ribs. Frankfurter swine corned beef meatloaf, strip steak.");
            }

            @Override
            public int getItemCount() {
                return 30;
            }
        });
    }// on create method END

    private static class ViewHolder extends RecyclerView.ViewHolder {
        TextView text1;
        TextView text2;

        public ViewHolder(View itemView) {
            super(itemView);
            text1 = itemView.findViewById(android.R.id.text1);
            text2 = itemView.findViewById(android.R.id.text2);
        }
    }
}

Solution

  • I think one thing might be that you kept the ViewHolder outside the RecyclerViewAdapter class. Keep the ViewHolder inside it or try keeping both the TextViews public.


    After typing all this I realized that it doesn't answer your question so I'll just keep it below in case you would want to refer to it later.

    So it won't work the way you have it. You need to have an ArrayList containing objects which will each contain info for what to place. I'll add code to explain better:

    In your example each cell only contains a TextView, so create a model class and name it whatever you want.

    public class Model{
    
       //Variable that will store the text
       private String text;
    
       //Constructor for the text
       public Model(String text){ 
          this.text = text;
       }
    
       //Add setters and getters for the text variable as well.
       public String getText(){return text}
    
       public void setText(String text){
           this.text = text;
       }
    }
    

    This model will contain the info to be displayed. Now create an ArrayList in your RecyclerView class:

    //You can initialize in Constructor as well.
    private ArrayList<Model> cellsList = new ArrayList<>(); 
    
    //Set the ArrayList
    public void setList(ArrayList<Model> list){
    
       cellsList = list;
       notifyDataSetChanged();
    }
    
    

    Finally, within the onBindViewHolder method, set the attributes for each cell using the items in the ArrayList.