How to get a variable from an Activity in the RecyclerAdapter of a RecyclerView?

3

I have a problem calling a variable that is in my MainActivity towards a RecyclerAdapter The variable I call it like this:

MainActivity main = new MainActivity();
String userName = main.userName;

But it returns me as null In my MainActivity I declare it like this:

public String userName;

And in the onCreate () method:

userName = getIntent().getStringExtra("name");
setUserName(userName);

I do not know what I have wrong I leave you my Repositorio

    
asked by Topi hernandez 03.06.2016 в 17:26
source

3 answers

1

You are creating a new object (MainActivity) and have tried to access an element that does not exist in that object. You have several options, the simplest of all is to pass that variable directly to the adapter since you are collecting it before creating it. For this, an option would be:

Change the adapter's constructor for:

   public RecyclerAdapter(List<Partido> partidos, String username){
        this.partidos = partidos;
        this.username = username;
    }

And when you believe in MainActivity:

adapter = new RecyclerAdapter(partidos, username);
    
answered by 03.06.2016 / 17:50
source
1

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 !!

    
answered by 03.06.2016 в 18:10
0

The answer of amendezcabrera explains to you very well because it is null, another possible solution is passing the activity by the constructor of the adapter:

Activity:

   adapter = new RecyclerAdapter(this, partidos);

Adapter:

  MainActivity activity;
  PartidosDataSource dataSource;
public RecyclerAdapter(MainActivity activity, List<Partido> partidos){
    this.activity = activity;
    this.partidos = partidos;
    Log.i("LOGTAG", "En recycler adapter: " + partidos.toString());
}

Then you can now do String userName = activity.userName; without problem.

If you need to use the same adapter for several activities you can do it like this:

Adapter:

  Activity activity;
  PartidosDataSource dataSource;
public RecyclerAdapter(Activity activity, List<Partido> partidos){
    this.activity = activity;
    this.partidos = partidos;
    Log.i("LOGTAG", "En recycler adapter: " + partidos.toString());
}


String userName = (MainActivity)activity).userName;

Greetings.

    
answered by 03.06.2016 в 18:23