2

我有一個數據庫並且數據庫填充了一個列表視圖。當我長按列表視圖中的列表項時,我得到一個上下文菜單,我可以選擇編輯。當點擊編輯選項時,我打開一個活動並從數據庫中獲取各個字段的所有值。現在,我想從數據庫中獲取一個值並將其顯示在微調器中。微調已經有了這些值,並從數據庫中得到填充。我曾嘗試以下,但我得到一個錯誤說我不能投了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 
+0

你也可以從querySub方法和錯誤日誌發佈代碼嗎? – Yury

+0

@Yury新增了.. ..請參閱..謝謝.. – ark

回答

3

我也遇到了類似的問題。 問題:我必須將微調器設置爲特定的字符串值,而不知道微調器控件本身的字符串值是多少。我創建的工作是採取微調控制並循環顯示其文本值。在每個位置比較spinners文本值和我想要設置的給定字符串值。如果它匹配,那麼得到當前位置並根據該位置設置我的紡紗器選擇。

int spinnerPosition = 0; 

for (int i = 0; i < yourSpinner.getCount(); i++) 
{ 
    Cursor cur = (Cursor)(yourSpinner.getItemAtPosition(i)); 

    //--When your bind you data to the spinner to begin with, whatever columns you 
    //--used you will need to reference it in the cursors getString() method... 

    //--Since "getString()" returns the value of the requested column as a String-- 
    //--(In my case) the 4th column of my spinner contained all of my text values 
    //--hence why I set the index of "getString()" method to "getString(3)" 

    String currentSpinnerString = cur.getString(3).toString(); 
    if(currentSpinnerString.contains("place string you want to set the spinner to here")) 
    { 
     //--get the spinner position-- 
     spinnerPosition = i; 
     break; 
     } 
} 

yourSpinner.setSelection(spinnerPosition); 
+0

工作很好...是否正是我的目標...我也想過這個,但無法成功實現它...謝謝你你的幫助.. :) – ark

+0

哦,是啊...使用'if(currentSpinnerString.equals(「你想設置微調到這裏的地方字符串))' 檢查確切的字符串或者如果你有一個字符串與列中的另一個值具有相同的字符,那麼它將得到包含子字符串的第一個值 – ark

+0

很高興能幫到我! – cking24343

相關問題