Android - 从动态返回数组的数据库函数中填充微调器

时间:2013-03-07 20:03:11

标签: android arrays sqlite spinner

我有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

1 个答案:

答案 0 :(得分: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);

您还可以使用List代替String[]