How to access ArrayList from another class in Android Java?

前端 未结 3 1977
情歌与酒
情歌与酒 2020-12-11 12:16

I\'m new to Android and Java but do have some experience in Objective C and iPhone programming. I\'m attempting to recreate an app I\'ve already designed for the iPhone and

3条回答
  •  生来不讨喜
    2020-12-11 12:39

    you can do it in two ways,

    1. Create a setter/getter class
    2. Make a public static method that returns ArrayList

    First Method:

    class name : myDataObject.java

    private ArrayList myArrayList;
    
    // setting the ArrayList Value 
    public void setArrayList ( ArrayList myArrayList )
    {
         this.myArrayList = myArrayList;
    }
    
    // getting the ArrayList value 
    public ArrayList getArrayList() 
    {
         return myArrayList;
    }
    

    Second Method:

    In ArrayList file, ( suppose class name is class A.java )

    private static ArrayList myArrayList = null; 
    
    ... 
    // assign arraylist 
    
    public static ArrayList getArrayList()
    {
          return myArrayList;
    }
    

    in the calling activity/class you can call it using following code,

    private ArrayList newArrayList = null;
    
    newArrayList = A.getArrayList();
    

提交回复
热议问题