如何从 Android Java 中的另一个类访问 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12047775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access ArrayList from another class in Android Java?
提问by kev
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.
我是 Android 和 Java 新手,但在 Objective C 和 iPhone 编程方面有一些经验。我正在尝试重新创建一个我已经为 iPhone 设计的应用程序,但我被困在一个简单的概念上。
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();
)
在我的 ParserHandler 类中,我正在解析来自服务器的 XML 并将数据放入三个单独的 ArrayList 中。解析似乎工作正常。当我登录并遍历 ParserHandler.java 类中的 ArrayList 时,一切正常。(List1.java类有一些字符串变量,我已经宣布它在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
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:
dogArray 的声明如下:public ArrayList<List1> dogArray = new ArrayList<List1>();
现在我想从 DogListView.java 类访问 dogArray,因此在 onCreate 方法中我尝试执行以下操作:
@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);}
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.
我尝试了两种不同的方法,并且总是在日志中返回“0”。然而,当日志来自 ParserHandler.java 类时,总是会记录正确的大小并且所有数据都在那里。
This is the accessor method in ParserHandler.java.
这是 ParserHandler.java 中的访问器方法。
public int getNumberofDogs(){
return dogArray.size();
}
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.
我更喜欢通过访问器方法访问 dogArray(因为这似乎是我收集到的最佳实践)但是我愿意接受所有建议。
Thanks in advance!!
提前致谢!!
EDIT 8/23/12
编辑 8/23/12
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
我最终通过声明我的ArrayLists Static
. 我知道这种(和public
)方法不是 OOP 的理想选择,但我正在接受它。在我的 ParserHandler.java 我声明
public static ArrayList<List1> dogArray = null;
public static ArrayList<List1> otherArray = null;
public static ArrayList<List1> catArray = null;
Then begin my SAX parser:
然后开始我的 SAX 解析器:
@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();
}
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.
然后我的 SAX 解析的其余部分发生了。最后,从我想访问数组的任何类中,我只需以静态方式执行此操作ParserHandler.dogArray.size()
即可获取数组的大小。我现在可以从我需要的任何类中以任何我认为合适的方式操作数组。
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[]
.
我仍然不清楚为什么使用我解析的 ArrayLists 创建 ParserHandler 类的实例对我不起作用,因为当我使用简单的int[]
.
hopefully this can help someone else in the future.
希望这可以在未来帮助其他人。
Thanks again for everyones feedback!
再次感谢大家的反馈!
Cheers!
干杯!
回答by Lucifer
you can do it in two ways,
你可以通过两种方式做到这一点
- Create a setter/getter class
- Make a public static method that returns ArrayList
- 创建一个 setter/getter 类
- 创建一个返回 ArrayList 的公共静态方法
First Method:
第一种方法:
class name : myDataObject.java
类名: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 )
在 ArrayList 文件中,(假设类名是 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();
回答by Byter
You should not make the Methods static.Because that is not an OOP Design then.
您不应该将方法设为静态。因为那不是 OOP 设计。
There are 2 ways:
有2种方式:
1). Either make the properties public. (Not a good practise either)
1)。要么公开属性。(也不是一个好习惯)
2). add getters and setters for ParserHandler
class
2)。为ParserHandler
类添加 getter 和 setter
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
现在访问 dogArray 像这样
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,因为它是一个空列表。先使用setter方法设置数组
回答by Macchiato
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.
请注意,在您的 oncreate 中,您正在 ParserHandler 中执行文件操作,它将 xml 文件解析为您的数据。如果 ParserHandler 不在单独的线程中处理,这可能会阻塞 UI 线程。但是,如果您在单独的线程中处理,那么您的 int d = ph.getNumberofDogs(); 由于 UI 线程和处理解析的单独线程之间的竞争条件,即使您的 xml 中有数据,也可能返回 0。
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
将此添加到您的 ParserHandler 类中
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.
确保在完成解析 xml 数据后调用 mListener.onParsingDone。
In Your onCreate()...
在你的 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
}
});