0

我正在开发一个症状检查器 android 应用程序,用户可以在其中检查他可能有的许多症状。请检查我到目前为止所做的尝试:

   public String getData(String[] symptoms) {
    String search = "";
    Cursor c = myDataBase.query(DB_TABLE, new String[] 
            {KEY_CONDITIONS}, KEY_SYMPTOMS + "= ? ", symptoms, null, null, null);

    c.moveToFirst();

    while (c.isAfterLast() == false) {
        search += c.getString(0) + ", ";
        c.moveToNext();
    }

    return search;

}

但我需要动态生成 where 字符串,因为我不知道会选择多少症状。我将如何做到这一点?您的帮助将不胜感激。谢谢!

4

1 回答 1

0

诀窍是您的数组中的每个字符串都必须一个插入字符 ( ?) 。

一种解决方案是使用循环:

String where = KEY_SYMPTOMS + " = ?";
String relation = " OR "; /* Maybe " AND "? */

StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
    builder.append(relation).append(where);

Cursor c = myDataBase.query(DB_TABLE, new String[] 
        {KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null);

您还可以编写一个更短但等效的循环:

while (c.moveToNext())
    search += c.getString(0) + ", ";  // A StringBuilder should be faster
于 2013-03-01T17:32:42.430 回答