问题描述
我知道如何从一个活动传递一个变量到另一个使用全局变量。
I know how to pass a variable from one activity to another using global variables.
例如。
在one.java:
e.g.In one.java:
GlobalVars.setColour(0);
在two.java:
if (GlobalVars.getColour() == 0) ...
GlobalVariables.java:
GlobalVariables.java:
private static int colour2;
public static int getColour() {
return colour2;
}
public static void setColour(int colour) {
colour2 = colour;
}
如果我有one.java一个数组,我需要它在另一个类?
What if i have an array in one.java and i need it in another class?
ArrayList<String> myArr = new ArrayList<String>();
myArr,该上载有从手机的通讯录中的联系人,因此它是动态的。我需要它来上传一个ListView其在自定义对话框类的元素。如何将它传递给另一个活动/对话?
myArr is uploaded with contacts from the phone book of the phone, so it is dynamic. I need it to upload a ListView with its elements in a custom dialog class. How to pass it to another activity/dialog?
推荐答案
您所选择的方法(创建一个静态实例)可以工作像的ArrayList
中的一个对象同样的方式,你做了与原始(这是建立一个单身)。
The method you have chosen (creating a static instance) WILL work for an object like ArrayList
in the same fashion as you did with the primitive (this is creating a Singleton).
然而,在大多数情况下,创建静态字段只传递活动之间的数据是绝对不推荐使用。双方的基本数据和的ArrayList&LT;弦乐方式&gt;
可以作为意图用于启动另一个活动额外传递
However, in most cases creating static fields just to pass data between Activities is definitely not recommended. Both primitive data and ArrayList<String>
can be passed as extras in the Intent you use to start another Activity.
private ArrayList<String> mArray;
private String mString;
private int mValue;
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("arrayListExtra", mArray);
intent.putExtra("stringExtra", mString);
intent.putExtra("intExtra", mValue);
startActivity(intent);
所有这些数据类型(及以上)的可以在意图
无缝传递。然后,您可以在另一边访问它们如下:
All of these data types (and more) can be seamlessly passed in an Intent
. Then, you can access them on the other side as follows:
Intent intent = getIntent();
ArrayList<String> array = intent.getStringArrayListExtra("arrayListExtra");
String string = intent.getStringExtra("stringExtra");
int value = intent.getIntExtra("intExtra", 0);
如果您正在传递数据的对话框,你可以调用一个setter方法并传递任何你想要的,无需担心该活动之间存在的边界。例如,使用自定义对话框实现的方法在对话框中,以便您可以显示它之前设置的值。
If you are passing the data to a Dialog then you can call a setter method and pass anything you want without worrying about the boundaries that exist between Activities. For instance, with a custom Dialog implement a method in your dialog so you can set the value before displaying it.
public class MyDialog extends Dialog {
private ArrayList<String> mItems;
//All other methods of the dialog here
public void setItems(ArrayList<String> items) {
mItems = items;
}
}
然后,在你的任何活动的方法,你打算创建和显示对话框做
Then, in whichever method of your Activity you plan to create and show the Dialog do
//theArray is your ArrayList<String> with data.
MyDialog dialog = new MyDialog();
dialog.setItems(theArray);
dialog.show();
注意,这是你将如何通过项目清单到AlertDialog显示。这些对话框也有像 setItems方法()
来在显示它之前传递数据。如果你的活动为您管理的对话框(你调用的ShowDialog()
从活动),请致电 onCreateDialog)的制定者(
或上prepareDialog()
...两者是比较合适的。
Notice that this is how you would pass a list of items to an AlertDialog for display. These dialogs also have methods like setItems()
to pass data in before showing it. If you're Activity manages the dialog for you (you're calling showDialog()
from the Activity), call the setter in onCreateDialog()
or onPrepareDialog()
...whichever is more appropriate.
希望帮助!
这篇关于Android的数组作为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!