How to access ArrayList from another class in Android Java?

前端 未结 3 1976
情歌与酒
情歌与酒 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:44

    You should not make the Methods static.Because that is not an OOP Design then.

    There are 2 ways:

    1). Either make the properties public. (Not a good practise either)

    2). add getters and setters for ParserHandler class

    class ParserHandler {
    
      private  List dogArray = new ArrayList();
    
      public List getDogArray() {
       return this.dogArray;
      }
    
      public void setDogArray(List dogArray) {
        this.dogArray = dogArray;
      }
    
    
    }
    

    Now access dogArray Like this

    ph.getDogArray();
     int m = ph.getDogArray().size();
    

    Initially it will be 0 since it is an empty list. Use the setter method to set the array first

提交回复
热议问题