两个activivty之间传递数组(转)

简介: 两个activivty之间传递数组(转)

Java代码

public class Home extends Activity {   
  
           
  
        public static final String ARRAYS_COUNT = "com.yourname.ARRAYS_COUNT";   
  
        public static final String ARRAY_INDEX = "com.yourname.ARRAY_INDEX";   
  
           
  
        protected void onCreate(Bundle savedInstanceState) {   
  
                super.onCreate(savedInstanceState);   
  
                   
  
                final String data[][] = new String[][] {{"1","pavan"},{"2","kumar"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};   
  
                Bundle bundle = new Bundle();   
  
                int count = data.length;   
  
                bundle.putInt(ARRAYS_COUNT, ARRAY_INDEX );   
  
                for (int i = 0; i < count; i++)   
  
                        bundle.putStringArray(ARRAY_INDEX + i, data[i]);   
  
                Intent intent = new Intent(this, Second.class);   
  
                intent.putExtras(bundle);   
  
                startActivity(intent);   
  
                    
  
        }    
  
           
  
}   
  
  
  
public class Second extends Activity {   
  
           
  
        protected void onCreate(Bundle savedInstanceState) {   
  
                super.onCreate(savedInstanceState);   
  
                   
  
                Bundle bundle = getIntent().getExtras();   
  
                   
  
                if (bundle != null) {   
  
                        int count = bundle.getInt(Home.ARRAYS_COUNT, 0);   
  
                        ArrayList<String[]> arrays = new ArrayList<String[]>(count);   
  
                        for (int i = 0; i < count; i++)   
  
                                arrays.add(bundle.getStringArray(Home.ARRAY_INDEX + i));   
  
                        String[][] data = arrays.toArray(new String[][]{});   
  
                }   
  
        }   
  
           
  }   

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

方式有很多,如果你不想new Bundle来传

调用intent的intent.putExtra(name, value)两次就可以了

String[] str1 = {"aa","aa"};
        String[] str2 = {"bb","bb"};
        Intent intent = new Intent();
                intent.putExtra("dev1", str1);
                intent.putExtra("dev2", str2);
                intent.setClass(this, OtherActivity.class);
        startActivity(intent);

在取数据的OtherActivity中

   

String[] dev = this.getIntent().getStringArrayExtra("dev1");
        String[] div = this.getIntent().getStringArrayExtra("dev2");

就拿到了字符串

相关文章
|
5天前
如何实现数组和 List 之间的转换?
如何实现数组和 List 之间的转换?
|
5天前
2020-10-10 数组和对象的区分方法
2020-10-10 数组和对象的区分方法
|
5月前
|
C# 数据安全/隐私保护
C# 窗体之间参数互相传递的两种方法与使用
C# 窗体之间参数互相传递的两种方法与使用
|
算法 搜索推荐 编译器
一组类型相同的数据【C 数组】总结
一组类型相同的数据【C 数组】总结
|
API C#
我不想再传递 nameof 了
有的时候抛出一个异常,我们需要知道是哪个方法抛出的异常。那么,我们可以通过传递 nameof 来获取调用者的方法名。但是,感觉很烦,每次都要传递 nameof。那么,有没有更好的方法呢?
64 0
我不想再传递 nameof 了
|
C语言
打印100到200之间的单数。打印100到200之间的双数(函数方法)
打印100到200之间的单数。打印100到200之间的双数(函数方法)
81 0
打印100到200之间的单数。打印100到200之间的双数(函数方法)
|
编译器
传递数组给函数
传递数组给函数
78 0
|
编译器
详解函数的三种传递方式
详解函数的三种传递方式
126 0
|
C++
C++ 用引用的方式向函数传递数组
C++ 用引用的方式向函数传递数组
125 0
C++ 用引用的方式向函数传递数组
|
Serverless C语言
C 语言中关于通过形参传递数组的长度计算的一些思考
C 语言中关于通过形参传递数组的长度计算的一些思考 一 背景 学习 C 语言的过程中,计算数组的长度经常会碰到。在字符型的数组中我们可以使用 strlen() 来获取当前数组的长度,对于其他类型的数组,这个方法就不适用了。
2728 0