-1

Why do we use something like this to populate the recyclerview with data? Can't we just create a simple array of string like String[] titles and display them, like in ListView?

List< Information> data

public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current = data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
}

I recently watched a video on how to work with recyclerview and I can't understand why we have to create another class of information and then again create its array inside onBindViewHolder to show data.

Can anybody help me understand this code?

Jonnus
  • 2,988
  • 2
  • 24
  • 33
Sudeep
  • 37
  • 1
  • 6
  • What do you mean? You can use `List<>` and`ArrayList<>` just fine in `ListView`. In fact, that would be preferred over a non-flexible Array. – TheLettuceMaster Jan 04 '16 at 21:38

2 Answers2

2

If you take a look at this example by google, they used an array dataset to populate data in RecyclerView. Take a look at Google's docs:

Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

Note that unlike ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use getAdapterPosition() which will have the updated adapter position. Override onBindViewHolder(ViewHolder, int, List) instead if Adapter can handle effcient partial bind.

Mohammad Fatemi
  • 1,278
  • 2
  • 12
  • 16
0
String [] title ; 
int [] imageID;
onBindViewHolder(MyViewHolder holder, int position) { 
 Information current = data.get(position); 
 holder.title.setText(title [position]); holder.icon.setImageResource(imageID     [position]); 
}
Rahul Pareta
  • 786
  • 6
  • 18
  • 1
    May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT Jan 07 '17 at 00:18
  • Will remember next time – Rahul Pareta Jan 09 '17 at 17:41