【问题标题】:Passing ArrayList of string arrays from one activity to another in android在android中将字符串数组的ArrayList从一个活动传递到另一个活动
【发布时间】:2013-07-01 10:57:03
【问题描述】:

我想在 Android 中将字符串数组的 ArrayList 从一个活动传递到另一个活动。
如何使用intentbundle?请考虑intent.putStringArrayListExtra 在这种情况下不起作用,因为它不适用于字符串数组。

【问题讨论】:

标签: java android android-intent arraylist android-activity


【解决方案1】:

不知道你所说的“字符串数组的ArrayList”是什么意思

如果您有字符串数组,请查看以下链接

Passing string array between android activities

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

检索

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

public Intent putStringArrayListExtra (String name, ArrayList&lt;String&gt; value)

向意图添加扩展数据。名称必须包含包前缀,例如应用程序 com.android.contacts 将使用类似“com.android.contacts.ShowAll”的名称。

参数

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

返回

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

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

检索

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

【讨论】:

    【解决方案2】:

    我不熟悉这是否可行,因为 Bundle 类的情况如此,所以我将实现一个自定义对象来容纳您的 ArrayList。这是一个很好的干净解决方案,用于容纳您需要在两个活动中访问的其他常见数据

    public class MyCustomData implements Serializable {
    
        static final long serialVersionUID = 42432432L;
        public ArrayList<String[]> strings = new ArrayList<String[]>();
    
        public MyCustomData() {
    
        };
    }
    

    然后在您的活动中:

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

    我也建议使用数组列表的数组列表而不是数组的数组列表

    【讨论】:

      【解决方案3】:

      所以你想传递一个字符串数组列表,而不是字符串列表,对吧?由于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");
      

      【讨论】:

        【解决方案4】:

        这个问题有一个简单的解决方案,您可以简单地在您的第一个活动中声明 ArrayList public & static,然后在另一个活动中调用它。

        在您的第一个活动中

        public static ArrayList<String> myList = new ArrayList();
        
        myList.add("some_value");
        

        在第二个活动中

        Toast.makeText(getApplicationContext(),FirstActivity.myList.size().toString(),Toast.LENGTH_SHORT).show();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多