我创建了一个 SpinnerActivity
,如下所示:
http://img4.fotos-hochladen.net/uploads/bildschirmfotovse8j4po1t.png
现在,例如,如果我选择“Landschaft”(英语:景观),我想在 DatabaseHandler.java
中搜索“Landschaft”(景观)类别中的位置
因此我在DatabaseHandler.java
中编写了以下方法:
在这个方法中,我刚刚编写了Kategorie = Landscape。
但是我希望将 Spinner 中选定的 SpinnerArray
(Landschaft、Brücken...等)插入到我的 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"
替换为您选择的变量。
关于java - 如何实现 String 变量而不是 'Landschaft' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917988/