Spinner displaying first item text while other item is selected(在选择其他项目时显示第一个项目文本的微调按钮)
本文介绍了在选择其他项目时显示第一个项目文本的微调按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个微调控件,它由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:截图:
推荐答案
将spinner.setSelection(cursor.getPosition())
更改为spinner.setSelection(cursor.getPosition(),true)
起到了作用。
这篇关于在选择其他项目时显示第一个项目文本的微调按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在选择其他项目时显示第一个项目文本的微调按钮


猜你喜欢
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01