java将数组定义为全局,Android的数组作为全局变量

I know how to pass a variable from one activity to another using global variables.

e.g.

In one.java:

GlobalVars.setColour(0);

In two.java:

if (GlobalVars.getColour() == 0) ...

GlobalVariables.java:

private static int colour2;

public static int getColour() {

return colour2;

}

public static void setColour(int colour) {

colour2 = colour;

}

What if i have an array in one.java and i need it in another class?

ArrayList myArr = new ArrayList();

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?

解决方案

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).

However, in most cases creating static fields just to pass data between Activities is definitely not recommended. Both primitive data and ArrayList can be passed as extras in the Intent you use to start another Activity.

private ArrayList 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 array = intent.getStringArrayListExtra("arrayListExtra");

String string = intent.getStringExtra("stringExtra");

int value = intent.getIntExtra("intExtra", 0);

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 mItems;

//All other methods of the dialog here

public void setItems(ArrayList items) {

mItems = items;

}

}

Then, in whichever method of your Activity you plan to create and show the Dialog do

//theArray is your ArrayList with data.

MyDialog dialog = new MyDialog();

dialog.setItems(theArray);

dialog.show();

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.

Hope that helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值