Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing ArrayList of string arrays from one activity to another in android

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.

like image 286
user2547447 Avatar asked Jul 03 '13 16:07

user2547447


2 Answers

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]);
}
like image 106
Raghunandan Avatar answered Oct 16 '22 09:10

Raghunandan


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

like image 26
MrTristan Avatar answered Oct 16 '22 09:10

MrTristan