0

我正在尝试将 ContentValues 对象与数据库中的值进行比较,但我无法弄清楚为什么以下内容不起作用:

ContentValues werte = new ContentValues();
try{
    JSONObject ClickedItem = new JSONObject();
    ClickedItem = resultArray.getJSONObject(position);
    werte.put("name", ClickedItem.getString("name"));
    werte.put("hersteller", ClickedItem.getString("hersteller"));
}
catch (Exception e) {
    System.out.println("Whoops - something went wrong!3");
    e.printStackTrace();
}

Boolean allreadyAdded = false;
Cursor devicesCursor = mDatenbank.query("addedDevices", new String[] {"name", "hersteller"}, null, null, null, null, null);
startManagingCursor(devicesCursor);
devicesCursor.moveToFirst();

for (int i=0;i<devicesCursor.getCount();i++){

    if(devicesCursor.getString(0) == werte.getAsString("name")){
        allreadyAdded = true;
    }
    devicesCursor.moveToNext();
}

if(allreadyAdded==false){                       
    mDatenbank.insert("addedDevices", null, werte);
}

只是不会工作,if(devicesCursor.getString(0) == werte.getAsString("name"))我不知道为什么。System.out.println 向我展示了 devicesCursor 和 are 都具有相同的值,但已添加不会设置为 true...

4

1 回答 1

1

因为您不能使用==运算符比较两个字符串,所以您的 if 条件永远不会成立。

要比较两个字符串,只需使用.equals().equalsIgnoreCases()来自Java String Class ..

if(devicesCursor.getString(0).equals(werte.getAsString("name")))
于 2013-01-10T11:45:33.613 回答