0

database充满了某些值,我想知道如何将它们放入spinner. 我有一个名为的类SetSql,它包含我所有的数据库函数,其中有一个名为 的函数getProfiles(),它返回一个String Array. 现在,我的spinner页面上也有一个 ID 为 的spinner1,我想知道如何从该getProfiles()函数中填充它?

另外我必须注意,数据库中的项目数是未知的,范围可以从 0 到几乎任何值。

谢谢!


我的努力

    SetSql getprofiles = new SetSql(this);
    getprofiles.open();
    if(getprofiles.getProfiles().length>0){
        spinnerArray = new String[100];
        spinnerArray = getprofiles.getProfiles();
    }else{
        spinnerArray = new String[1];
        spinnerArray[0] = "No Profiles, Please Create One";
    }
    getprofiles.close();

    ArrayAdapter<String> array = new ArrayAdapter<String>(UserActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
    array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    profiles.setAdapter(array);

我的 getProfiles()

public String[] getProfiles() throws SQLException{
    String[] columns = new String[]{KEY_USER};
    Cursor c = ourDatabase.query(TABLE_USERS, columns, null, null, null, null, KEY_ROWID);
    String[] result = new String[100];

    int iName = c.getColumnIndex(KEY_USER);
    int count = 0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
        result[count] = c.getString(iName);
        count++;
    }
    return result;
}

请记住,此函数是类的一部分,因此 KEY_Xs 和 ourDatabase 等一些变量不在此处。

LOGCAT

03-07 23:03:25.691: E/AndroidRuntime(10791): FATAL EXCEPTION: main
03-07 23:03:25.691: E/AndroidRuntime(10791): java.lang.NullPointerException
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.View.measure(View.java:8313)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.View.measure(View.java:8313)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.View.measure(View.java:8313)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.View.measure(View.java:8313)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.os.Looper.loop(Looper.java:130)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-07 23:03:25.691: E/AndroidRuntime(10791):    at dalvik.system.NativeStart.main(Native Method)

如果您知道为什么会崩溃,我很想听听:D

4

1 回答 1

0

尝试这样的事情:

String[] myArray = {"some","values"};

Spinner spin = (Spinner)findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,myArray);

spin.setAdapter(adapter);

您也可以使用 aList而不是String[]

于 2013-03-07T20:09:54.993 回答