If in the MainAcivity.java
you execute the RecyclerView
, what you would do would be to pass that variable through the constructor of RecyclerAdapter
:
In your RecyclerAdapter
you create a global variable that stores the value of the variable you pass and creates a constructor:
private String userName;
public RecyclerAdapter(String userName) {
this.userName = userName;
}
And in your MainActivity.java
you initialize it like this:
private RecyclerAdapter recyclerAdapter;
private RecyclerView recyclerView;
public String userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = getIntent().getStringExtra("name");
setUserName(userName); // No se para que servirá en tu código,
// si aplicando esto te sigue devolviendo
// null, miraría a ver si me borra el valor.
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerAdapter = new RecyclerAdapter(userName);
[...]
}
And so you can use the variable in your RecyclerAdapter
.
In case that RecyclerAdapter
re-use it in other parts of your code, you can create an empty constructor and thus be able to choose whether or not to pass data to it.
I hope it serves you.
Good luck !!