How to access ArrayList from another class in Android Java?

前端 未结 3 1974
情歌与酒
情歌与酒 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();
    
    0 讨论(0)
  • 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<List1> dogArray = new ArrayList<List1>();
    
      public List<List1> getDogArray() {
       return this.dogArray;
      }
    
      public void setDogArray(List<List1> 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

    0 讨论(0)
  • 2020-12-11 12:45

    Note that in your oncreate you are doing a file operation in your ParserHandler which parses the xml file as your data. This could potentially block the UI thread if the ParserHandler is not processed in a separate thread. However if you processed in a separate thread then your int d = ph.getNumberofDogs(); may return 0 even if there are data in your xml because of race conditions between UI thread and the separate thread processing the parsing.

    The best solution in my opinion is to create a listener when the parsing is done so that you are pretty sure that the processing is done before you access the size of the list.

    add this in your ParserHandler class

    class ParserHandler {
    
           ...... your original codes here
    
           private OnParsingDoneListener mListener;
           public void setOnParsingDoneListener (OnParsingDoneListener listener){
                 mListener = listener;
           }
    
          public static interface OnParsingDoneListener {
                public void onParsingDone (List dogList);
          }
    
    }
    

    make sure to call mListener.onParsingDone when youre done parsing xml data.

    In Your onCreate()...

    ParserHandler ph = new ParserHandler();
    ph.setOnParsingDoneListener (new ParserHandler.OnParsingDoneListener(){
         public void onParsingDone(List dogList){
             // do whatever you want to the doglist
             // at this point all parsing is done and dogList contains the data from xml
         }
    });
    
    0 讨论(0)
提交回复
热议问题