两个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");

就拿到了字符串

目录
打赏
0
2
2
1
83
分享
相关文章
|
8月前
|
函数\函数先后关系
函数\函数先后关系
46 1
|
9月前
|
LabVIEW局部变量和值属性节点之间的区别
LabVIEW局部变量和值属性节点之间的区别
167 0
|
9月前
2020-10-10 数组和对象的区分方法
2020-10-10 数组和对象的区分方法
|
9月前
|
列表、元组和字典之间的区别是什么
列表、元组和字典之间的区别是什么
102 0
数组与字符串的关系【了解一下】
数组与字符串的关系【了解一下】
137 0
我不想再传递 nameof 了
有的时候抛出一个异常,我们需要知道是哪个方法抛出的异常。那么,我们可以通过传递 nameof 来获取调用者的方法名。但是,感觉很烦,每次都要传递 nameof。那么,有没有更好的方法呢?
108 0
我不想再传递 nameof 了
详解函数的三种传递方式
详解函数的三种传递方式
228 0
传递数组给函数
传递数组给函数
105 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等