我正在尝试将两个微调器中的字符串“año”和“artista”传递给下一个活动,我得到的值是“año”,但“artista”总是无效的。我使用这种方法来获取其他活动中的值,并且它可以工作但是在这里,“artista”总是无效的。而且,我尝试过切换方法而没有成功。
我希望有人帮助我并告诉我我做错了什么。我在寻求帮助之前阅读并查找信息,
这是我的代码,
System.out.println("Author List: " + authorList);
如果有人可以帮助我,
提前致谢,
答案 0 :(得分:0)
可能你试图用另一个名字捕捉一串意图。
试试这段代码:
<强> SpinnerTest.java 强>
public class SpinnerTest extends Activity implements AdapterView.OnItemSelectedListener {
String year;
String artist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_activity);
Spinner spinnerYear = (Spinner) findViewById(R.id.spinner1);
final String[] years = {"1995", "2000", "2005"};
spinnerYear.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, years));
spinnerYear.setOnItemSelectedListener(this);
Spinner spinnerArtists = (Spinner) findViewById(R.id.spinner2);
final String[] artists = {"Michael Jackson", "U2", "Depeche Mode"};
spinnerArtists.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, artists));
spinnerArtists.setOnItemSelectedListener(this);
Button btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent searchIntent = new Intent(SpinnerTest.this, SearchActivity.class);
searchIntent.putExtra("year", year);
searchIntent.putExtra("artist", artist);
startActivity(searchIntent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (adapterView.getId()){
case R.id.spinner1:
year = adapterView.getSelectedItem().toString();
Toast.makeText(SpinnerTest.this, year, Toast.LENGTH_LONG).show();
break;
case R.id.spinner2:
artist = adapterView.getSelectedItem().toString();
Toast.makeText(SpinnerTest.this, artist, Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
<强> SearchActivity.java:强>
public class SearchActivity extends ActionBarActivity {
String year;
String artist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_spinner_value);
TextView txtYear = (TextView) findViewById(R.id.txtYear);
TextView txtArtist = (TextView) findViewById(R.id.txtArtist);
if(getIntent().hasExtra("year")){
year = getIntent().getStringExtra("year");
txtYear.setText(year);
}
if(getIntent().hasExtra("artist")){
artist = getIntent().getStringExtra("artist");
txtArtist.setText(artist);
}
}
}
它对我有用