[英]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. 我想在Android中将字符串数组的ArrayList从一个活动传递到另一个活动。
How can I use intent
or bundle
? 我如何使用
intent
或bundle
? Please consider that intent.putStringArrayListExtra
is not working in this case, because it is not for string arrays. 请考虑在这种情况下,
intent.putStringArrayListExtra
不起作用,因为它不适用于字符串数组。
Not sure what you mean by "ArrayList of string arrays" 不确定你的意思是“字符串数组的ArrayList”
If you have string array then check the below link 如果您有字符串数组,请检查以下链接
Passing string array between android activities 在android活动之间传递字符串数组
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
ArrayList implements Serializable
ArrayList实现
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. 将扩展数据添加到intent。 The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
名称必须包含包前缀,例如app com.android.contacts将使用“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数组的ArrayList
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]);
}
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. 我不熟悉这是否可行,因为Bundle类的情况如此,所以我将实现一个自定义对象来容纳您的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 我也建议使用arraylists的arraylist而不是数组的arraylist
So you want to pass a List of String arrays, not a List of Strings, right? 所以你想传递一个String数组列表,而不是一个字符串列表,对吧? Since
ArrayList
is serializable, you can add it pretty easy: 由于
ArrayList
是可序列化的,因此您可以非常轻松地添加它:
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");
This Problem have a simple solution you can simply declare ArrayList public & static in your First activity and then call it in another one. 这个问题有一个简单的解决方案,您只需在First活动中声明ArrayList public&static,然后在另一个活动中调用它。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.