我有一个支柱,开始开展第二次价值变化活动。 这种做法是好的。
返回后,一切工作都得当。 但是,如果现在我轮值流动,那么奥特温斯当选的利比亚人就被打! 原因? 关键应放在第1位。 什么意味着不会打回。
How does android know which spinner item is selected? I tried to prevend to restore this information by sending the base class the a null value for the savedInstanceState.
这里有一些法典:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
setContentView(R.layout.menu);
String db=MyDatabase.getData();
if(db == null)
db="";
Spinner spinner=(Spinner)findViewById(R.id.spinner);
MyAdapter adapter=new MyAdapter(this, db, "ID", "Name");
spinner.setAdapter(adapter);
adapter.setHint(spinner, "Please select...");
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id2) {
int id=((SpinnerItem)parent.getAdapter().getItem(position)).getId();
Intent intent=new Intent();
intent.setClass(MainMenu.this, Other.class);
intent.putExtra("id", id);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
这里是我的适应者:
public class MyAdapter extends ArrayAdapter<SpinnerItem> {
private final Context context;
private String hint;
public MyAdapter(Context context, String input, String key, String value) {
super(context, android.R.layout.simple_spinner_item, create(input, key, value));
this.context=context;
}
private static SpinnerItem[] create(String input, String key, String value) {
Vector<SpinnerItem> list=new Vector<SpinnerItem>();
// fill the list
return list.toArray(new SpinnerItem[] {});
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// show the hint
if(position == -1) {
TextView tv=new TextView(context);
tv.setTextColor(Color.DKGRAY);
tv.setText(hint);
return tv;
}
return super.getView(position, convertView, parent);
}
public void setHint(Spinner spinner, String hint) {
if(spinner.getAdapter() == null) {
throw new IllegalStateException("Set your adapter first!");
}
this.hint=hint;
try {
final Method m=AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
m.setAccessible(true);
m.invoke(spinner, -1);
final Method n=AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
n.setAccessible(true);
n.invoke(spinner, -1);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}