Java 使用 Intent.putExtra 发送数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3848148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending arrays with Intent.putExtra
提问by Kitinz
I have an array of integers in the activity A:
我在活动 A 中有一个整数数组:
int array[] = {1,2,3};
And I want to send that variable to the activity B, so I create a new intent and use the putExtra method:
我想将该变量发送到活动 B,因此我创建了一个新意图并使用 putExtra 方法:
Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);
In the activity B I get the info:
在活动 BI 中获取信息:
Bundle extras = getIntent().getExtras();
int arrayB = extras.getInt("numbers");
But this is not really sending the array, I just get the value '0' on the arrayB. I've been looking for some examples but I didn't found anything so.
但这并不是真正发送数组,我只是在 arrayB 上得到值“0”。我一直在寻找一些例子,但我没有找到任何东西。
采纳答案by Mark B
You are setting the extra with an array. You are then trying to get a single int.
您正在使用数组设置额外内容。然后你试图获得一个整数。
Your code should be:
你的代码应该是:
int[] arrayB = extras.getIntArray("numbers");
回答by paladiya chirag
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);
}
回答by Khalid Habib
This code sends array of integer values
此代码发送整数值数组
Initialize array List
初始化数组列表
List<Integer> test = new ArrayList<Integer>();
Add values to array List
将值添加到数组列表
test.add(1);
test.add(2);
test.add(3);
Intent intent=new Intent(this, targetActivty.class);
Send the array list values to target activity
将数组列表值发送到目标活动
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);
startActivity(intent);
here you get values on targetActivty
在这里你可以得到 targetActivty 的值
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");