Android 向一个活动传递多个数据?
我正试图在一个Android 向一个活动传递多个数据?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我正试图在一个Intent中传递多个数据项: if (strActStat == "Sedentary") { // passactStat.putString("keySedentary", strActStat); // passSeden.putString("keyMale", gender); i = new Intent(CalorieTrackerTargetWeight.this, TargetWeightResults.class); i.put
Intent
中传递多个数据项:
if (strActStat == "Sedentary") {
// passactStat.putString("keySedentary", strActStat);
// passSeden.putString("keyMale", gender);
i = new Intent(CalorieTrackerTargetWeight.this, TargetWeightResults.class);
i.putExtra("keyGender", gender);
i.putExtra("keyAct", strActStat);
//i.putExtra("keyAct", strActStat);
startActivity(i);
}
为什么这样不行?为什么我不能在一个
Intent
中传递多个项目?您不能将字符串与=
进行比较
if (strActStat.equals("Sedentary")) { // should work
编辑:
@Hesam已经写了一个非常详细的答案,但他的解决方案并不实用。不要使用ArrayList
,而应该坚持使用putExtra(键,值)
。为什么?与ArrayList解决方案相比,它有一些优势:
put()
的顺序与get()
的顺序相同。请考虑以下情况:通常发送3个值,但在某些情况下不希望发送第二个值。当您使用ArrayList解决方案时,您最终会发送null
作为第二个值,以确保第三个值将保留在原来的位置。这是非常混乱的编码!相反,您应该只发送两个值,当接收活动尝试接收第二个值时,它可以按照自己的意愿处理返回的null。。。例如,将其替换为默认值键应在接收活动中声明为常量。因此,通过查看这些常量,您总是知道活动可以处理哪些意图数据。这是很好的编程李>
希望这有助于澄清一些意图用法。您不能将字符串与=
进行比较
if (strActStat.equals("Sedentary")) { // should work
编辑:
@Hesam已经写了一个非常详细的答案,但他的解决方案并不实用。不要使用ArrayList
,而应该坚持使用putExtra(键,值)
。为什么?与ArrayList解决方案相比,它有一些优势:
您不限于ArrayList的类型
您不必在列表中保持静态顺序。由于只能使用索引值来获取列表,因此需要确保put()
的顺序与get()
的顺序相同。请考虑以下情况:通常发送3个值,但在某些情况下不希望发送第二个值。当您使用ArrayList解决方案时,您最终会发送null
作为第二个值,以确保第三个值将保留在原来的位置。这是非常混乱的编码!相反,您应该只发送两个值,当接收活动尝试接收第二个值时,它可以按照自己的意愿处理返回的null。。。例如,将其替换为默认值
钥匙的命名会让你知道里面应该有什么
您的键应在接收活动中声明为常量。因此,通过查看这些常量,您总是知道活动可以处理哪些意图数据。这是很好的编程李>
希望这有助于澄清使用意图。我认为这不是唯一的问题,首先,如果(strActStat==“久坐”)
这是错误的。您不能用这种方式与字符串进行比较。因为通过这种方式,对象不是在比较字符串。正确的方法是if(strActStat.equalIgnoreCase(“久坐”)
如果您使用,那么您可以在一个目的中传递多个数据
也可以使用ArrayList
。
以下是您需要的代码框架:
申报表
private List<String> test;
私有列表测试;
在适当的位置初始化列表
test = new ArrayList<String>();
test=newarraylist();
并根据需要添加数据以进行测试
传递给意向书如下:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
ArrayList<String> test = data.getStringArrayListExtra("test");
Intent-Intent=getIntent();
意图.PutstringArrayListXTRA(“测试”,(ArrayList)测试);
按如下方式检索数据:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
ArrayList<String> test = data.getStringArrayListExtra("test");
arraylisttest=data.getStringArrayListExtra(“测试”);
希望有帮助。我认为这不是唯一的问题,首先,如果(strActStat==“久坐”)
这是错误的。您不能用这种方式与字符串进行比较。因为通过这种方式,对象不是在比较字符串。正确的方法是if(strActStat.equalIgnoreCase(“久坐”)
如果您使用,那么您可以在一个目的中传递多个数据
也可以使用ArrayList
。
以下是您需要的代码框架:
申报表
private List<String> test;
私有列表测试;
在适当的位置初始化列表
test = new ArrayList<String>();
test=newarraylist();
并根据需要添加数据以进行测试
传递给意向书如下:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
ArrayList<String> test = data.getStringArrayListExtra("test");
Intent-Intent=getIntent();
意图.PutstringArrayListXTRA(“测试”,(ArrayList)测试);
按如下方式检索数据:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
ArrayList<String> test = data.getStringArrayListExtra("test");
arraylisttest=data.getStringArrayListExtra(“测试”);
希望有帮助。试试这个:
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
namevalue=name.getText().toString();
overvalue=over.getText().toString();
audiostatus=audio.getText().toString();
Intent intent=new Intent(Settings.this,home.class);
Bundle bundle = new Bundle();
bundle.putString( "namevalue",namevalue);
bundle.putString("overvalue",overvaluse);
bundle.putInt("value",variablename);
intent.putExtras(bundle);
startActivity(intent);
}
});
试试这个:
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
namevalue=name.getText().toString();
overvalue=over.getText().toString();
audiostatus=audio.getText().toString();
Intent intent=new Intent(Settings.this,home.class);
Bundle bundle = new Bundle();
bundle.putString( "namevalue",namevalue);
bundle.putString("overvalue",overvaluse);
bundle.putInt("value",variablename);
intent.putExtras(bundle);
startActivity(intent);
}
});
我也面临同样的问题。
我的错误是,我正在传输的一个变量没有初始化。
就像你的性别或strActStat一样。我也面临同样的问题。
我的错误是,我正在传输的一个变量没有初始化。
就像您的案例中的性别或strActStat一样。您可以传递多个额外数据。实际上,您在其中放置了两个不同的数据(性别和strActStat)。不能使用同一密钥添加数据!如果(strActStat==“久坐”)尝试@WarrenFaith answer,您可以传递多个额外数据。实际上,您在其中输入了两个不同的数据(性别和strActStat)。不能使用同一密钥添加数据!如果(strActStat==“seditional”)尝试@WarrenFaith answer详细回答,您这里的问题是:如果我可以使用putExtra(key,value)
,为什么我要使用ArrayList和额外的创建对象(和解析对象)的开销呢?我还有一个问题,就是我不能用钥匙接力。。。查看我更新的答案,了解一些优点/缺点详细的答案,但我为什么要使用ArrayLi