Passing ArrayList of string arrays from one activi

2019-01-12 04:35发布

I want to pass an ArrayList of string arrays from one activity to another in Android.
How can I use intent or bundle? Please consider that intent.putStringArrayListExtra is not working in this case, because it is not for string arrays.

4条回答
叼着烟拽天下
2楼-- · 2019-01-12 04:52

This Problem have a simple solution you can simply declare ArrayList public & static in your First activity and then call it in another one.

In your First Activity

public static ArrayList<String> myList = new ArrayList();

myList.add("some_value");

In second Activity

Toast.makeText(getApplicationContext(),FirstActivity.myList.size().toString(),Toast.LENGTH_SHORT).show();
查看更多
做个烂人
3楼-- · 2019-01-12 04:55

I'm not familiar with if this is doable or not as things stand with the Bundle class so i would implement a custom object to house your ArrayList. this is a good clean solution for housing other common data that you would need to access in both activities

public class MyCustomData implements Serializable {

    static final long serialVersionUID = 42432432L;
    public ArrayList<String[]> strings = new ArrayList<String[]>();

    public MyCustomData() {

    };
}

and then in your Activity:

MyCustomData myCustomDataInstance = new MyCustomData();
myCustomDataInstance.strings = //set it here;

Bundle bundle = new Bundle();
Intent selectedIntent = new Intent(getActivity(), MyNextClass.class);
bundle.putSerializable("key", myCustomDataInstance);
selectedIntent.putExtras(bundle);
startActivity(selectedIntent);

also i would suggest using an arraylist of arraylists instead of an arraylist of arrays

查看更多
Explosion°爆炸
4楼-- · 2019-01-12 05:06

Not sure what you mean by "ArrayList of string arrays"

If you have string array then check the below link

Passing string array between android activities

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

ArrayList implements Serializable

You can use intents

    ArrayList<String> mylist = new ArrayList<String>();  
    Intent intent = new Intent(ActivityName.this, Second.class);
    intent.putStringArrayListExtra("key", mylist);
    startActivity(intent);

To retrieve

    Intent i = getIntent();  
    ArrayList<String> list = i.getStringArrayListExtra("key");

public Intent putStringArrayListExtra (String name, ArrayList<String> value)

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters

name    The name of the extra data, with package prefix.
value   The ArrayList data value.

Returns

Returns the same Intent object, for chaining multiple calls into a single statement.

To pass ArrayList of String array

String[] people = {
        "Mike Strong",
        "Jennifer Anniston",
        "Tom Bennet",
        "Leander Paes",
        "Liam Nesson",
        "George Clooney",
        "Barack Obama",
        "Steve Jobs",
        "Larry Page",
        "Sergey Brin",
        "Steve Wozniak"
};
String[] people1 = {
        "raghu", 
        "hello"
};


ArrayList<String[]> list = new ArrayList<String[]>();
list.add(people);
list.add(people1);
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("key", list);
startActivity(i); 

To retrieve

Intent in = getIntent();
ArrayList<String[]> list =(ArrayList<String[]>) in.getSerializableExtra("key");
for(int i=0;i<list.size();i++)
{
   String s[]= list.get(i);
   for(int iv=0;iv<s.length;iv++)
   Log.i("..............:",""+s[iv]);
}
查看更多
够拽才男人
5楼-- · 2019-01-12 05:07

So you want to pass a List of String arrays, not a List of Strings, right? Since ArrayList is serializable, you can add it pretty easy:

ArrayList<String[]> list = new ArrayList<String[]>();
// Add some String[]
Intent i = new Intent();
i.putExtra("xxx", list);

// And to get it back
ArrayList<String[]> list = (ArrayList<String[]>) i.getSerializableExtra("xxx");
查看更多
登录 后发表回答