如何从另一个类在Android中Java访问ArrayList的? [英] How to access ArrayList from another class in Android Java?

查看:125
本文介绍了如何从另一个类在Android中Java访问ArrayList的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和Java,但确实有在目标C和iPhone编程经验。我试图重新创建一个应用程序,我已经专门为iPhone和我被陷在什么应该是一个简单的概念。

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 am getting stuck on what should be a simple concept.

在我ParserHandler类我解析从服务器的XML和把数据分成三个独立的ArrayList的。解析似乎是工作的罚款。当我登录,并通过该ArrayList在我ParserHandler.java类重复这一切工作正常。 (List1.java类有几个字符串变量,我已经声明它像这样在ParserHandler:私人List1中的thelist =新的List1();

In my ParserHandler class I am parsing an XML from a server and putting the data into three separate ArrayList's. The parsing appears to be working fine. When I log and iterate through the ArrayList within my ParserHandler.java class it all works fine. (List1.java class has a few string variables and I've declared it like so in the ParserHandler: private List1 theList = new List1(); )

for(int i = 0; i<dogArray.size(); i++){
        theList = dogArray.get(i);
        Log.i(TAG, "looping " + i + " " + theList.Name);
        Log.i(TAG, "looping " + i + " " + theList.PhotoUrl);
        Log.i(TAG, "looping " + i + " " + theList.Type);


        }//this loops fine and has all the data

该dogArray是像这样声明:公开的ArrayList&LT; List1的&GT; dogArray =新的ArrayList&LT; List1的&GT;(); 现在我想从类DogListView.java所以在onCreate方法我尝试做以下访问dogArray:

The dogArray is declared like so: public ArrayList<List1> dogArray = new ArrayList<List1>(); Now I want to access the dogArray from the class DogListView.java so in the onCreate method I attempt to do the following:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dog_list_view);
        ParserHandler ph = new ParserHandler();
        int d = ph.getNumberofDogs();
        int m = ph.dogArray.size();
        Log.i(TAG, "dog size is:" + d + "and:" + m);}

我尝试了两种不同的方式与这两个始终在日志中返回0。然而正确大小始终记录和所有的数据是存在的时候,日志来自ParserHandler.java类。

I've tried two different ways and both always return "0" in the log. However the correct size is always logged and all the data is there when the log comes from the ParserHandler.java class.

这是在ParserHandler.java的访问方法。

This is the accessor method in ParserHandler.java.

   public int getNumberofDogs(){
            return dogArray.size();
        }

我倒是preFER通过存取方法来访问dogArray(因为这似乎是最好的做法从我收集的),但是我接受的所有建议。

I'd prefer to access the dogArray via accessor method (as this seems to be best practice from what I've gathered) however I'm open to all suggestions.

在此先感谢!

修改12年8月23日

我最终宣布我的的ArrayList静态解决问题。我知道这(和公开)的方法我并不理想的面向对象的,但我要与它。在我ParserHandler.java我宣布

I ended up solving the problem by declaring my ArrayLists Static. I know this (and public) approach my not be ideal for OOP but i'm going with it. In my ParserHandler.java I declared

public static ArrayList<List1> dogArray = null; 
public static ArrayList<List1> otherArray = null;
public static ArrayList<List1> catArray = null;

然后开始我的SAX解析器:

Then begin my SAX parser:

@Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qName, attributes);
        if (qName.equalsIgnoreCase("ArrayOfDataFeedAnimal")){
            catArray = new ArrayList<List1>();
            dogArray = new ArrayList<List1>();
            otherArray = new ArrayList<List1>();
        }else if(qName.equalsIgnoreCase("DataFeedAnimal")){
            theList = new List1();
        }

那么我SAX解析东西剩下的事。最后,无论从任何类我想访问数组我只是做,在由 ParserHandler.dogArray.size的静态的方式()来获取数组的大小。我现在可以操纵数组中的任何方式,我认为合适的无论从那个班我需要得到它。

then the rest of my SAX parsing stuff happens. Lastly, from whatever class I want to access the array i simply do that in the static way by ParserHandler.dogArray.size() to get the size of the array. I can now manipulate the array any way i see fit from whatever class i need to get it.

我还不清楚为什么创建ParserHandler类的实例并没有为我工作与我分析的ArrayList,因为当它工作得很好,当我用一个简单的测试 INT []

I'm still unclear why creating an instance of the ParserHandler class hasn't worked for me with my parsed ArrayLists because when it worked fine when I tested with a simple int[].

希望这可以帮助别人的未来。

hopefully this can help someone else in the future.

再次感谢大家的反馈!

干杯!

推荐答案

您可以通过两种方式做到这一点,

you can do it in two ways,

  1. 创建一个setter /吸气类
  2. 请返回ArrayList的一个公共静态方法

第一种方法:

类名:myDataObject.java

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:

在ArrayList的文件,(假设类名是类A.java)

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

private static ArrayList myArrayList = null; 

... 
// assign arraylist 

public static ArrayList getArrayList()
{
      return myArrayList;
}

在调用活动/类,你可以使用下面的code调用它,

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

private ArrayList newArrayList = null;

newArrayList = A.getArrayList();

这篇关于如何从另一个类在Android中Java访问ArrayList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆