Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实现字符串变量而不是“Landschaft”?_Java_Android_Database_Sqlite_Android Sqlite - Fatal编程技术网

Java 如何实现字符串变量而不是“Landschaft”?

Java 如何实现字符串变量而不是“Landschaft”?,java,android,database,sqlite,android-sqlite,Java,Android,Database,Sqlite,Android Sqlite,我创建了一个SpinNeralActivity,如下所示: 现在,例如,如果我选择English:Landschaft,我想在DatabaseHandler.java中搜索“Landschaft”类别中的位置 因此,我在DatabaseHandler.java中编写了以下方法: 在这个方法中,我刚刚编写了Kategorie=Landscape 但是我想,被选中的斯宾纳雷·兰沙夫特、布鲁肯……等等。在我的示例中,微调器将插入到我的DatabaseHandler中,而不是Landschaft中,我如

我创建了一个SpinNeralActivity,如下所示:

现在,例如,如果我选择English:Landschaft,我想在DatabaseHandler.java中搜索“Landschaft”类别中的位置

因此,我在DatabaseHandler.java中编写了以下方法:

在这个方法中,我刚刚编写了Kategorie=Landscape

但是我想,被选中的斯宾纳雷·兰沙夫特、布鲁肯……等等。在我的示例中,微调器将插入到我的DatabaseHandler中,而不是Landschaft中,我如何才能做到这一点

public List<Position> getAllPositionsWithCategory() {
        List<Position> positionList = new ArrayList<Position>();

        String selectQuery = "SELECT * FROM " + TABLE_POSITIONS
                + " WHERE Kategorie = 'Landschaft'";

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {

            do {

                Position position = new Position();

                position.setID(Integer.parseInt(cursor.getString(0)));

                position.setName(cursor.getString(1));

                position.setKategorie(cursor.getString(2));

                position.setLaenge(cursor.getFloat(3));

                position.setBreite(cursor.getFloat(4));

                // Adding position to list

                positionList.add(position);

            } while (cursor.moveToNext());

        }
}

只需将固定字符串替换为?,并传递要插入db.rawQuery的第二个参数中的值

以下内容可能适用于您的案例:

String selectQuery = "SELECT * FROM " + TABLE_POSITIONS
            + " WHERE Kategorie = ?";
// ...
Cursor cursor = db.rawQuery(selectQuery, new String[] { "Landschaft" });
现在,您可以用自己选择的变量替换字符串Landschaft

另见: