有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SQLite数据库在调用时不工作

我使用SQLite数据库进行一个关于不同面条的项目。对于这个项目,我使用一个导航抽屉,并从数据库中调用数据。不知何故,当我试图查询数据时,抛出了一个SQLiteException,但什么也没发生

为什么不起作用

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noodle);
        //set up toolbar as the normal app bar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        SQLiteOpenHelper databaseHelper = new DatabaseHelper(this);
        ListView noodleListPerCategory = findViewById(R.id.selected_noodleList);

        try {
            database = databaseHelper.getReadableDatabase();
            fetch();

            //create the cursor adapter to fill the list view with values from the database
            SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, 安卓.R.layout.simple_list_item_1, cursor, new String[]{"NAME"},
                    new int[]{安卓.R.id.text1}, 0);
            noodleListPerCategory.setAdapter(listAdapter);
        } catch (SQLiteException e) {
            Toast toast = Toast.makeText(this, "Database is  not working!", Toast.LENGTH_SHORT);
            toast.show();
        }

        //show item detail using the listener when an item is clicked
        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> noodleListPerCategory, View view, int position, long id) {
                //starts DetailActivity
                Intent intent = new Intent(NoodleActivity.this, DetailActivity.class);
                intent.putExtra(DetailActivity.CHOSEN_NOODLE_ITEM, (int) id);
                startActivity(intent);
            }
        };
        //connects the listener to the list view
        noodleListPerCategory.setOnItemClickListener(itemClickListener);
    }

    @Override
         public void onDestroy(){
            super.onDestroy();
            cursor.close();
            database.close();
        }

    public Cursor fetch() {
        cursor = this.database.query("NOODLE", new String[]{"_id", "NAME"}, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

共 (0) 个答案