Skip to content

Pass data from activity to recyclerAdapter – Android

I want to pass the data from the Activity containing a recyclerview to it’s recyclerAdapter class. I just want to use a String in the adapter but I don’t know how to get it from the activity. Is there any way to do this? Please keep in mind I want data from Activity to Adapter and not vice a versa

Edit: So in my activity, I have defined a public method:

public String getName(){
return f_name;

Now how do I call this in my adapter class? I’m not able to access my getName() method here !



Agustine’s answer is for kotlin, here’s the java version

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>() {
    private String myString;
    private Context context;
    MyAdapter(Context context, String myString) {
        this.context = context;
        this.myString = myString;

and then in your activity

MyAdapter adapter = new MyAdapter(this, "string you want to pass to adapter")

That’s it. You can learn more about recyclerview and recyclerAdapter here

User contributions licensed under: CC BY-SA
10 People found this is helpful