use*_*447 5 java android arraylist android-intent android-activity
我想在Android中将字符串数组的ArrayList从一个活动传递到另一个活动.
我如何使用intent或bundle?请考虑intent.putStringArrayListExtra在这种情况下不起作用,因为它不适用于字符串数组.
Rag*_*dan 16
不确定你的意思是"字符串数组的ArrayList"
如果您有字符串数组,请检查以下链接
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
ArrayList实现 Serializable
你可以使用意图
ArrayList<String> mylist = new ArrayList<String>();
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
要检索
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("key");
Run Code Online (Sandbox Code Playgroud)
public Intent putStringArrayListExtra (String name, ArrayList<String> value)
将扩展数据添加到intent.名称必须包含包前缀,例如app com.android.contacts将使用"com.android.contacts.ShowAll"之类的名称.
参数
name The name of the extra data, with package prefix.
value The ArrayList data value.
Run Code Online (Sandbox Code Playgroud)
返回
Returns the same Intent object, for chaining multiple calls into a single statement.
Run Code Online (Sandbox Code Playgroud)
传递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);
Run Code Online (Sandbox Code Playgroud)
要检索
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]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23477 次 |
| 最近记录: |