如何从数据库中获取项的值并将其设置为微调器值

时间:2012-06-04 07:26:04

标签: android database spinner android-arrayadapter simplecursoradapter

我有一个数据库,数据库填充了listview。当我长按列表视图中的列表项时,我得到一个上下文菜单,我可以选择编辑。单击编辑选项时,我打开一个活动并从数据库中获取相应字段的所有值。现在,在这里,我想从数据库中获取一个值并在微调器中显示它。微调器已经有了这些值,并且正在从数据库中填充...我尝试了以下内容,但是我收到一条错误消息,说我无法将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

1 个答案:

答案 0 :(得分:3)

我也遇到过类似的问题。 问题:我必须将微调器设置为特定的字符串值,而不知道字符串值在微调控件本身中的位置。我创建的工作是采用微调控件并循环显示其文本值。在每个位置将微调器文本值与我想要设置的给定字符串值进行比较。如果匹配则获取当前位置并根据该位置设置我的微调器选择。

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);