使用Intent.putExtra发送数组

我在活动A中有一个整数数组:

int array[] = {1,2,3}; 

我想把这个variables发送给活动B,所以我创build了一个新的intent并使用putExtra方法:

 Intent i = new Intent(A.this, B.class); i.putExtra("numbers", array); startActivity(i); 

在BI活动中获取信息:

 Bundle extras = getIntent().getExtras(); int arrayB = extras.getInt("numbers"); 

但是这不是真的发送数组,我只是在arrayB上得到值“0”。 我一直在寻找一些例子,但我没有发现任何东西。

你正在设置额外的数组。 你然后试图得到一个int。

你的代码应该是:

 int[] arrayB = extras.getIntArray("numbers"); 

此代码发送整数值的数组

初始化数组List

 List<Integer> test = new ArrayList<Integer>(); 

将值添加到数组List

 test.add(1); test.add(2); test.add(3); Intent intent=new Intent(this, targetActivty.class); 

将数组列表值发送到目标活动

 intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test); startActivity(intent); 

在这里你可以获得targetActivty的值

 Intent intent=getIntent(); ArrayList<String> test = intent.getStringArrayListExtra("test"); 
 final static String EXTRA_MESSAGE = "edit.list.message"; Context context; public void onClick (View view) { Intent intent = new Intent(this,display.class); RelativeLayout relativeLayout = (RelativeLayout) view.getParent(); TextView textView = (TextView) relativeLayout.findViewById(R.id.textView1); String message = textView.getText().toString(); intent.putExtra(EXTRA_MESSAGE,message); startActivity(intent); }