Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recyclerView.setAdapter does not accept ArrayAdapter

Tags:

I am trying to populate RecyclerView with simple String data but recyclerView.setAdapter(adapter); gives error. What's the problem?

I am new to recyclerview.

RecyclerView recyclerView; String []data = {"ankush", "kapoor"};  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);     recyclerView.setLayoutManager(new LinearLayoutManager(this));     recyclerView.setAdapter(adapter); } 
like image 581
akkk Avatar asked Aug 12 '16 20:08

akkk


People also ask

Can we use ArrayAdapter for RecyclerView Android?

Adapter , much like the built-in Android ArrayAdapter , will populate the data into the RecyclerView . It also converts a Java object into an individual list item View to be inserted and displayed to the user.

What is ListAdapter?

Adapter base class for presenting List data in a RecyclerView , including computing diffs between Lists on a background thread. This class is a convenience wrapper around AsyncListDiffer that implements Adapter common default behavior for item access and counting.

How does ListAdapter work?

ListAdapter gets data using a method called submitList() , which submits a list to be diffed against the current list and displayed. This means you no longer have to override getItemCount() because ListAdapter manages the list. In the Activity class, call submitList() on the Adapter and pass in the data list.

What is Adapter in RecyclerView in android?

The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts.


1 Answers

You have to create a custom Adapter class for a RecyclerView. Here is the example of RecyclerView custom adapter class :

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{  private LayoutInflater inflater; private Context context; <your string array list>  public CustomAdapter(Context context,<your string arraylist>) {     inflater = LayoutInflater.from(context);     this.context = context;     this.<your string arraylist>=<your string arraylist>; }  @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     View view = inflater.inflate(R.layout.custom_layout, parent, false);     MyViewHolder holder = new MyViewHolder(view);     return holder; }  @Override public void onBindViewHolder(MyViewHolder holder, int position) {          holder.serial_number.setText(<your string array[position]>); }  @Override public int getItemCount() {     return <size of your string array list>; }  class MyViewHolder extends RecyclerView.ViewHolder {     TextView serial_number;      public MyViewHolder(View itemView) {         super(itemView);         serial_number = (TextView)itemView.findViewById(R.id.serialNo_CL);         }     } } 
like image 92
Deepak Kumar Avatar answered Sep 17 '22 13:09

Deepak Kumar