无法从另一个类加载的微调器数据中获取所选项

时间:2017-11-16 08:15:52

标签: android database sqlite spinner

我有两个类,即产品和类别,在产品类中,我有一个从类别类中读取数据的微调器。 我希望当从spinner中选择一个类别时,从categorydbAdapter中的方法获取id,但我在这一行中有错误:

get_cat_id = categoryDBAdapter.backcatID(selectedItem);

问题是什么? 这是我的代码:

  spinner = (Spinner) insert_product_dialog.findViewById(R.id.spinner_c);

            categoryDBAdapter = new CategoryDBAdapter(getApplicationContext());


            ArrayList<Category> listcat = categoryDBAdapter.display_categories();

            ArrayAdapter<Category> adapter =
                    new ArrayAdapter<Category>(getApplicationContext(), R.layout.snipper_dropdown, listcat);
            adapter.setDropDownViewResource(R.layout.snipper_dropdown);

            spinner.setAdapter(adapter);

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    String selectedItem = parent.getItemAtPosition(position).toString();

                    get_cat_id = categoryDBAdapter.backcatID(selectedItem);

                    test.setText(get_cat_id);

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

和categorydbadapter中的方法:

public int backcatID(String CatName) {

    int back_id;
    String query = "select * from " + TBL_CAT+" where "+KEY_TITLE_cat+"="+CatName;
    SQLiteDatabase database = this.getReadableDatabase();
    Cursor cursor = database.rawQuery(query, null);
    if (cursor.moveToNext()) {
         back_id = cursor.getInt(0);
    }
    else
    {
         back_id = 1000;
    }

    return back_id;
}

和错误:

11-16 12:19:51.259 3148-3148/com.myshop.navmenu E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.myshop.navmenu, PID: 3148
                                                              android.database.sqlite.SQLiteException: no such column: phone (code 1): , while compiling: select * from tbl_category where title=phone
                                                                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
                                                                  at com.myshop.navmenu.MyDatabase.CategoryDBAdapter.backcatID(CategoryDBAdapter.java:63)
                                                                  at com.myshop.navmenu.Activity.ProductManagerActivity$1$3.onItemSelected(ProductManagerActivity.java:134)
                                                                  at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
                                                                  at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
                                                                  at android.widget.AdapterView.-wrap1(AdapterView.java)
                                                                  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  11-16 12:19:51.339 1590-1604/system_process W/ActivityManager:   Force finishing activity com.myshop.navmenu/.Activity.ProductManagerActivity
   11-16 12:19:51.563 2256-2911/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
   11-16 12:19:51.675 1293-1326/? D/gralloc_ranchu: gralloc_alloc: format 1 and    usage 0x900 imply creation of host color buffer

1 个答案:

答案 0 :(得分:0)

使用它。

 public int backcatID(String CatName) {

        int back_id;
        String query = "select * from " + TBL_CAT+" where " + KEY_TITLE_cat + "=\""+ CatName + "\"";
        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor = database.rawQuery(query, null);
        if (cursor.moveToNext()) {
             back_id = cursor.getInt(0);
        }
        else
        {
             back_id = 1000;
        }

        return back_id;
    }