首页 > 其他分享 >两个activivty之间传递数组(转)

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

时间:2023-02-27 14:05:43浏览次数:29  
标签:String activivty Bundle 传递 intent bundle 数组 new Intent



Java代码 
1. public class Home extends Activity {
2.
3.
4.
5. public static final String ARRAYS_COUNT = "com.yourname.ARRAYS_COUNT";
6.
7. public static final String ARRAY_INDEX = "com.yourname.ARRAY_INDEX";
8.
9.
10.
11. protected void onCreate(Bundle savedInstanceState) {
12.
13. super.onCreate(savedInstanceState);
14.
15.
16.
17. final String data[][] = new String[][] {{"1","pavan"},{"2","kumar"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
18.
19. Bundle bundle = new Bundle();
20.
21. int count = data.length;
22.
23. bundle.putInt(ARRAYS_COUNT, ARRAY_INDEX );
24.
25. for (int i = 0; i < count; i++)
26.
27. bundle.putStringArray(ARRAY_INDEX + i, data[i]);
28.
29. Intent intent = new Intent(this, Second.class);
30.
31. intent.putExtras(bundle);
32.
33. startActivity(intent);
34.
35.
36.
37. }
38.
39.
40.
41. }
42.
43.
44.
45. public class Second extends Activity {
46.
47.
48.
49. protected void onCreate(Bundle savedInstanceState) {
50.
51. super.onCreate(savedInstanceState);
52.
53.
54.
55. Bundle bundle = getIntent().getExtras();
56.
57.
58.
59. if (bundle != null) {
60.
61. int count = bundle.getInt(Home.ARRAYS_COUNT, 0);
62.
63. ArrayList<String[]> arrays = new ArrayList<String[]>(count);
64.
65. for (int i = 0; i < count; i++)
66.
67. arrays.add(bundle.getStringArray(Home.ARRAY_INDEX + i));
68.
69. String[][] data = arrays.toArray(new String[][]{});
70.
71. }
72.
73. }
74.
75.
76. }
77. Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

  1. 方式有很多,如果你不想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");
    就拿到了字符串

标签:String,activivty,Bundle,传递,intent,bundle,数组,new,Intent
From: https://blog.51cto.com/u_15070324/6088386

相关文章

  • leetcode 862. 和至少为 K 的最短子数组
    一个双端单调队列:如果新加入的数比队列尾的数小,那么队列尾的数就可以丢去,这是因为如果未来的一个数能和队列尾的数满足条件,那么也一定可以和新加入的数满足条件。另外,如果......
  • Mybatis参数传递
    一、多个参数以后接口参数是多个时,在每个参数上都使用@Param注解。这样代码的可读性更高。二、单个参数POJO类型:直接使用。要求属性名和参数占位符名称一致。M......
  • python Numpy数组2.27
    #成员类型转换arr.astype(np.float_)#转换数组对象成员的类型为float,形状不变。#形状转换arr.resize(shape)#返回值是一个None,不能引用内部的属性arr.reshape(shape)#......
  • 122. 糖果传递(贪心)
    https://www.acwing.com/problem/content/description/124/求最小代价,且数据范围为1e6,大概是O(N)或O(NlogN),大概就是排个序,贪心一般都是排序设定每个小朋友给出的为xi,有如......
  • 递减元素使数组呈锯齿状
    //给你一个整数数组 nums,每次 操作 会从中选择一个元素并 将该元素的值减少 1。如果符合下列情况之一,则数组 A 就是 锯齿数组:每个偶数索引对应的元素都大于相......
  • 改变原数组和不改变原数组
    改变原数组:unshift:向数组开头添加元素push:向数组末尾添加元素,并返回新的长度unshift:向数组开头添加元素,并返回新的长度shift:将第一个元素删除并且返回删除元素re......
  • WebAssembly之数据交换(数组)
    因为工作需要,又倒腾起了WebAssembly,这次主要探索(解决)的问题是Array类型数据的传递经过咨询以及与ChatGPT的沟通,目前有了如下两种方案:1.通过类型化数组传递1//c++2......
  • [LeetCode]4. 寻找两个正序数组的中位数
    给定两个大小分别为m和n的正序(从小到大)数组nums1和nums2。请你找出并返回这两个正序数组的中位数。算法的时间复杂度应该为O(log(m+n))。分别查找两个数组的......
  • 数组与自定义函数
    一维数组    ·                  字符可以做下标吗?数组的下标必须是整数,那么字符可以做下标吗?比如:int a[255];a[......
  • 稀疏数组SparseArray
    稀疏数组SparseArray1.稀疏数组介绍当一个数组中的大部分元素都为0,或者大部分元素均为同一个值时,此时记录了很多没有意义的数据,可以用稀疏数组来保存该数组。在稀疏数......