我有一個數據庫並且數據庫填充了一個列表視圖。當我長按列表視圖中的列表項時,我得到一個上下文菜單,我可以選擇編輯。當點擊編輯選項時,我打開一個活動並從數據庫中獲取各個字段的所有值。現在,我想從數據庫中獲取一個值並將其顯示在微調器中。微調已經有了這些值,並從數據庫中得到填充。我曾嘗試以下,但我得到一個錯誤說我不能投了SimpleCursorAdapter到ArrayAdapter ..如何從數據庫中獲取項目的值並將其設置爲微調值
String osub = cursor.getString(Database.INDEX_SUBJECT);
Cursor cs = mDBS.querySub(osub);
String subs = cs.getString(DatabaseSub.INDEX_SSUBJECT);
if(cs!=null){
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) subSpinner.getAdapter();
//cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(subs);
//set the default according to value
subSpinner.setSelection(spinnerPosition);
請告訴我如何做到這一點..謝謝..我比較新,所以請讓我知道,如果我忽略了一些東西。
謝謝..
編輯
querySub
方法
public Cursor querySub(String sub) throws SQLException {
Cursor cursor = mDatabase.query(true, DATABASE_TABLE, sAllColumns, "ssub like " + "'" + sub + "'", null, null, null, null, null);
if (cursor.moveToNext()) {
return cursor;
}
cursor.moveToFirst();
return cursor;
}
Error Log
06-05 12:11:14.144: E/AndroidRuntime(775): FATAL EXCEPTION: main
06-05 12:11:14.144: E/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{an.droid.kit/an.droid.kit.DetailsActivity}: java.lang.ClassCastException: android.widget.SimpleCursorAdapter cannot be cast to android.widget.ArrayAdapter
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.os.Looper.loop(Looper.java:137)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-05 12:11:14.144: E/AndroidRuntime(775): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 12:11:14.144: E/AndroidRuntime(775): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 12:11:14.144: E/AndroidRuntime(775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-05 12:11:14.144: E/AndroidRuntime(775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-05 12:11:14.144: E/AndroidRuntime(775): at dalvik.system.NativeStart.main(Native Method)
06-05 12:11:14.144: E/AndroidRuntime(775): Caused by: java.lang.ClassCastException: android.widget.SimpleCursorAdapter cannot be cast to android.widget.ArrayAdapter
06-05 12:11:14.144: E/AndroidRuntime(775): at an.droid.kit.DetailsActivity.dbToUI(DetailsActivity.java:217)
06-05 12:11:14.144: E/AndroidRuntime(775): at an.droid.kit.DetailsActivity.onCreate(DetailsActivity.java:104)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.Activity.performCreate(Activity.java:4465)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-05 12:11:14.144: E/AndroidRuntime(775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-05 12:11:14.144: E/AndroidRuntime(775): ... 11 more
你也可以從querySub方法和錯誤日誌發佈代碼嗎? – Yury
@Yury新增了.. ..請參閱..謝謝.. – ark