编码:
SecondActivity.java:
String open_time;
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
open_time = bundle.getString("open_time");
}
修改:我已尝试fromSpinner.setPrompt(open_time);
。但这对我没用。
答案 0 :(得分:0)
Android Spinners需要适配器。所以你应该为它们准备一个简单的适配器。
String[] list = new String[] {open_time};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
staticSpinner.setAdapter(adapter);
答案 1 :(得分:0)
我使用下面的代码解决了这个问题:
int s3;
s3 = fromSpinnerAdapter.getPosition(open_time);
fromSpinner.setSelection(s3);
答案 2 :(得分:0)
试试此代码
private ArrayList<String> list;
private int selectedItem = -1;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
staticSpinner.setAdapter(adapter);
staticSpinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Spinner selectedSpinner = (Spinner) adapterView;
switch (selectedSpinner.getId()) {
case R.id.sp_actions:
String row_data = sp_actions.getSelectedItem();
Log.d(TAG, "" + row_data);
// pass these variable to new activity and call setSelected method
selectedItem = list.indexOf(row_data);
break;
}
}