0

I have such problem.

I want to get arraylists from Java classes and join to one main arraylist in fragment.

public ArrayList<Coin getAll() {

     ArrayList<Coin alllist = new ArrayList<();

     alllist.addAll( I want this from java class getArraylist1());
     alllist.addAll( I want this from another java class getArraylist2());

     return alllist;

}

getArraylist1() and getArraylist2() this are array list in other classes. And I want to make on main arraylist.

Arraylists are not static

public ArrayList<Coin> getArraylist1()

        {
            ArrayList<Coin> coins = new ArrayList<>();

            Coin coadd = new Coin();

            coadd.setCoinid("RC2022");
            coadd.setCoinimages(R.drawable.fivrubl1798);
            coadd.setDenom("5");
            coins.add(coadd);

        return coins;
    }
  • you are doing it right ? then what is your question ? – Ravi Sep 23 '17 at 11:49
  • By other "classes" do you mean other `Activities`? – Rohan Stark Sep 23 '17 at 11:59
  • the question is not fully clear, maybe you want to know how to perform type oneness, but from what is in the code: just initiating `Coin` class list inside some `getArrayList` method from other classes should be enough... – Guillotine Sep 23 '17 at 12:05
  • Possible duplicate of [Is it possible to add an array or object to SharedPreferences on Android](https://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android) – ישו אוהב אותך Sep 23 '17 at 13:09
  • It doesn't seem that the issue is related with presented code, the answers are pretty clear. For further details you need to provide the error message or/and rest of the code. – Guillotine Sep 23 '17 at 13:26

1 Answers1

0

You can create objects of other classes and call get methods, e.g.:

public ArrayList getAll(){
    ArrayList<Coin> list = new ArrayList<>();
    YourClass yourObject = new YourClass();
    AnotherClass anotherObject = new AnotherClass();
    list.addAll(yourObject.getArraylist1());
    list.addAll(anotherObject.getArraylist2());
    return list;
}

If getArraylist1 and getArraylist2 methods are static then you don't need to create objects, e.g.:

public ArrayList getAll(){
    ArrayList<Coin> list = new ArrayList<>();
    list.addAll(YourClass.getArraylist1());
    list.addAll(AnotherClass.getArraylist2());
    return list;
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102