3

I want to access a list from another class then put it inside my RecyclerViewAdapter object .

The first class which contains the list.

public class Class1 {

    // The List : 

    List<Model1> mlisto = new ArrayList<>(); 
    mlisto.add(new Model1("HOLA","Dep17",R.drawable.img));
    mlisto.add(new Model1("bonjour","Dep17",R.drawable.img));
    mlisto.add(new Model1("hi","Dep17",R.drawable.img));
}

The second class which access the list from the first class.

public class Class2 { 

    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,new Class1().mlisto); 

}

When I use new Class1().mlisto it returns nothing like an empty list and the RecyclerView doesn't show anything on the app.

Son Truong
  • 13,661
  • 5
  • 32
  • 58
Skrille Ford
  • 63
  • 1
  • 7

3 Answers3

0

You can access the list like this :

public class Class1{

    private List<Model> mList;
    public List<Model1> getList(){

         mList = new ArrayList<>();
         mList.add(new Model1("HOLA","Dep17",R.drawable.img));
         mList.add(new Model1("bonjour","Dep17",R.drawable.img));
         mList.add(new Model1("hi","Dep17",R.drawable.img));
         return mList
    }
}

And in second class, you can use it like this:-

RecyclerViewAdapterrr adapter = new RecyclerViewAdapterrr(this,new Class1().getList()); 

setAdapter() is important

your_recyclerview_object.setAdapter(adapter);
Shaifali Rajput
  • 1,279
  • 12
  • 30
Abhishek Sharma
  • 271
  • 2
  • 11
0

you can use interfaces

create an interface inside class1 like this:

public interface CustomListListener{
    void onListChanged(List<Model1> myList);
}

create a property of listener inside Class1:

CustomListListener mListener;

add a public function for initializing the listener inside Class1:

public static void setOnListChangeListener(CustomListListener listener){
    this.mListener = listener;
}

implement the listener inside Class2:

public class Class2 implements Class1.CustomListListener { 
}

when you implement listener inside Class2, you will get a function inside class2 like this :

@Override
public void onListChanged(List<Model1> myList) {
    // in here you will access the list that you created or changed inside class1
}

when you wanna send the myList data to class2: 1- initiate the mListener property : for example you can initiate the mListener inside OnCreate Function of class

 Class1.setOnListChangeListener(this)
 this refers to class2 context

2- send the data to class2 inside your class1, whenever you want to update the list use mListener property like this :

mListener.onListChanged(myList);

this like you can change the list whenever you want and get the updated value inside onListChanged function, also you can add more functions inside your CustomListListener for different scenarios like removing items, adding items and etc...

Russell Ghana
  • 2,963
  • 3
  • 20
  • 31
0

You can use sharedPreference to store the list and can access it anywhere in your project by using its key. The best approach is shared in this link TinyDb Example

Shahkar Raza
  • 355
  • 3
  • 11