我知道有几个类似于我的问题,我已经尝试了现有问题中提到的所有建议的解决方案,但它仍然无效。很确定我在逻辑上做错了什么但是无法弄清楚在哪里。请指出正确的方向。
Spinner活动代码方法
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent main = new Intent (this, MainActivity.class);
Bundle myBundle = new Bundle();
String chosenAppType = appliancestypespinner.getSelectedItem().toString();
myBundle.putString("appliance type spinner",chosenAppType);
main.putExtra("chose appliance type", myBundle);
startActivity(main);
}
TextView Activity
(将微调器值作为textview
中的字符串(tvApplianceType = textview
)
Bundle myBundle = this.getIntent().getExtras();
if(myBundle == null)
{
return;
}
String str_recieved_appType = myBundle.getString("appliance type spinner");
if (str_recieved_appType != null)
{
tvApplianceType.setText(str_recieved_appType);
}
}
只是补充说我还将Spinner活动中存在的Edittext值传递给同一个textview,我将spinner值传递给EditText到TextView工作正常。它不起作用,因为我对两个操作都使用相同的textview,但是任何一个操作都需要同时工作:/。因此,要么将texttext编辑为textView,要么将微调框渲染为textview。
编辑文字代码
String str_appliance_type = et_input_Appliance_Type.getText().toString().trim();
if (!str_appliance_type.equals(""))
{
data.add(str_appliance_type );
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data );
appliancestypespinner.setAdapter(aa);
//passing edittext value to MainActivity class
Bundle carrier = new Bundle();
Intent i = new Intent (this, MainActivity.class);
i.putExtra("appliance type",str_appliance_type);
startActivity(i);
}
将Editext的值传递给textview
//getting the edittext value from inputAppliance class
Bundle carrier = getIntent().getExtras();
if(carrier == null)
{
return;
}
String str_recieved = carrier.getString("appliance type");
if (str_recieved != null)
{
tvApplianceType.setText(str_recieved);
}
答案 0 :(得分:1)
String str = appliancestypespinner.getSelectedItem().toString(); // I assume this line is proper.
Intent i = new Intent (this, MainActivity.class);
Bundle b = new Bundle();
b.putString("your_key", str);
i.putExtras(b);
startActivity(i);
在MainActivity中
Bundle b = getIntent().getExtras();
String s = b.getString("your_key");
your_textView.setText(s);
答案 1 :(得分:0)
更改此
main.putExtra("chose appliance type", myBundle);
startActivity(main);
到
main.putExtras(myBundle);
startActivity(main);
答案 2 :(得分:0)
Intent main = new Intent (this, MainActivity.class);
main.putExtra("SELECTED_DATA", appliancestypespinner.getSelectedItem().toString());
startActivity(main);
在另一项活动中:
String data=this.getIntent.getStringExtra("SELECTED_DATA");
答案 3 :(得分:0)
尝试使用Intent:
Spinner活动:
Intent calculate = new Intent(getApplicationContext(),Second.class);
calculate.putExtra("carry_name", entername.getText().toString());
关于第二项活动:
Intent intent = getIntent();
String name = intent.getStringExtra("carry_name");
congrates.setText("Congratulation " + name);