我正在开发一个项目,用户从微调器中选择最多五个选项,我想在按下提交按钮时在新活动中显示所选选项我在按下按钮时打开了新活动,但没有任何显示,我想要帮助我需要做的步骤,这是我的第二个屏幕的代码,显示数据
public class SecondScreenActivity extends Activity {
TextView FirstIwi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_iwi);
FirstIwi = (TextView) findViewById(R.id.spinner1);
Intent intename = getIntent();
String iwi1 = (String) intename.getSerializableExtra("USERNAME");
//Spinner spinnername2 = (Spinner) findViewById(R.id.spinner2);
FirstIwi.setText("First Iwi" + iwi1);
}
}
头等
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
public class IwiSelect extends Activity {
Spinner spinner1, spinner2, spinner3, spinner4, spinner5;
Button btnSubmit;
String iwi1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iwiselect);
addListenerOnButton();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_iwi_select, menu);
return true;
}
// get the selected drop down list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
spinner5 = (Spinner) findViewById(R.id.spinner5);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
iwi1 = spinner1.toString();
//Starting a new Intent
Intent nextScreen = new Intent(IwiSelect.this, SecondScreenActivity.class);
//Sending data to another Activity
nextScreen.putExtra("USERNAME", iwi1);
//nextScreen.putExtra(spinner2.getContext().toString(), false);
// starting new activity
startActivity(nextScreen);
}
});
}
}
答案 0 :(得分:2)
您需要使用以下代码:
// find spinner object here
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
//get selected value here
String itemStr = spinnername1.getSelectedItem().toString();
//put selected vale and start new activity
Intent mIntent = new Intent(ActivityOne.this, NewActivity.class);
mIntent.putExtra("data", itemStr);
startActivity(mIntent);
/**
* AT receiver side activity in onCreate
*/
Intent i = getIntent();
String dataString = i.getString("data");
答案 1 :(得分:0)
如果这是您正在使用的实际代码,
String name1 = i.getStringExtra("");
错了。放在()
中的内容应该是您在Intent
中添加额外内容时使用的关键值。如果你有
myIntent.putExtra("myKey", myValue)
然后当你从Intent
获得额外的资金时,你需要使用
String name1 = i.getStringExtra("myKey");
这将为您提供myValue
的价值。为每个key
使用不同的extra
。
如果没有这样做,那么请发布您发送Intent
数据的方式以及您似乎丢失值的位置
答案 2 :(得分:0)
试试这段代码。
Spinner spinnername1 = (Spinner) findViewById(R.id.spinner1);
String mStr = spinnername1.getSelectedItem().toString();
将价值传递给意图
Intent mIntent = new Intent(CurrentActivity.this, NextActivity.class);
mIntent.putExtra("Value", mStr);
startActivity(mIntent);
从意图中获取价值。
Bundle bdl = getIntent().getExtras();
String mValue = bdl.getString("Value");