我正在尝试使用 SimpleCursorAdapter 填充微调器。但是微调器完全是空的;它只是不显示项目。我没有收到错误消息。
几个小时以来,我一直试图弄清楚我做错了什么。我真的希望你能帮助我。
这是我活动中的 onCreate 函数:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
spinner = (Spinner) findViewById(R.id.spinner);
fillSpinner();
}
这是应该填充微调器的函数:
private void fillSpinner() {
Db = new DbAdapter(this);
Db.open();
final Cursor cursor = Db.getCats(true,true);
Log.d("fillSpinner","cursor.getCount: "+cursor.getCount()+" / cursor.getColumnCount: "+cursor.getColumnCount());//returns 8 and 4
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);
//...
cursor.close();
Db.close();
}
这是我的 DbAdapter 类中的 getCats() 函数:
public Cursor getCats(boolean times, boolean adapter) {
String id = KEY_ID;
if(adapter){
id = KEY_ID +" "+_ID;
}
String columns = id+", "+KEY_NAME;
if(times){
columns+=", "+KEY_START+", "+KEY_END;
}
return Db.rawQuery("SELECT "+columns+" FROM "+CATS_TABLE+" ORDER BY "+KEY_ID+" ASC", null);
}
我的常数:
private static final String CATS_TABLE = "cats";
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_START = "start";
public static final String KEY_END = "end";
public static final String _ID = "_id";
提前致谢!
耶洛斯