Android array as global variable

只谈情不闲聊 提交于 2019-12-23 05:33:27

问题


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<String> myArr = new ArrayList<String>();

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?


回答1:


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

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();

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!




回答2:


If you want to sent data from one Activity to another, you should use an Intent. For example:

Intent intent = new Intent().setClass(getApplicationContext(), CollegeDetails.class);
intent.putExtra("key", objectvalue);
startActivity(intent);



回答3:


Sounds like an ideal opportunity for a content provider in my opinion

Take a look here if unfamiliar with them: http://developer.android.com/guide/topics/providers/content-providers.html




回答4:


You will want to make an instance of the class you want to use in the other class then access the array.

Public Class MyClass 
{
  public ArrayList<String> myArr = new ArrayList<String>(); 
}

Public Class MyClassTwo
{
  private myClassInstance = new MyClass();
  myClassInstance.myArr = "hello world";
}


来源:https://stackoverflow.com/questions/5069694/android-array-as-global-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!