我具有由所述的onResume()方法内的SimpleCursorAdapter填充一个微调。 该选择也被设置在的onResume: spinner.setSelection(x)
当我去到另一个活动,然后返回到本次活动中,微调显示,而不是所选项目的文本的第一个项目的文本。
我该如何解决?
编辑:这是我的代码:
@Override
public void onResume(){
super.onResume();
fillSpinner();
}
private void fillSpinner() {
Db = new DbAdapter(this);
Db.open();
final Cursor cursor = Db.getCats(true,true);
startManagingCursor(cursor);
String[] from = new String[]{DbAdapter.KEY_NAME};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
cursor.moveToPosition(pos);
spinnerval = cursor.getInt(cursor.getColumnIndexOrThrow(DbAdapter._ID));
Log.d("spinnerval", spinnerval+"");
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setSelection(0);
int now = hour*60*60+minute*60;
Log.d("fillSpinner","now="+now+" / "+now/60.0/60.0);
cursor.moveToFirst();
while(cursor.moveToNext()){
int s = cursor.getInt(cursor.getColumnIndexOrThrow(DbAdapter.KEY_START));
int e = cursor.getInt(cursor.getColumnIndexOrThrow(DbAdapter.KEY_END));
if(e<s){
if(now>s){
e+=24*60*60;
}
else{
s-=24*60*60;
}
}
if(s<=now&&e>now){
spinner.setSelection(cursor.getPosition());
}
}
Db.close();
spinner.invalidate();
}
EDIT2:截图: