Android 哈希图问题

Android Hashmap issue

我有一段代码基本上可以在在线数据库之间同步数据。但是,我在特定代码行 (map.put("id", obj.get(mydb.WEB_ID).toString());) 上收到错误,其中从 android sqlite 数据库获取整数值并将其提交到在线数据库。完整内容如下:

public void updateSQLite(String response){
    ArrayList<HashMap<String, String>> syncL;
    syncL = new ArrayList<HashMap<String, String>>();
    // Create GSON object
    Gson gson = new GsonBuilder().create();
    try {
        // Extract JSON array from the response
        JSONArray arr = new JSONArray(response);
        System.out.println(arr.length());
        // If no of array elements is not zero
        if(arr.length() != 0){
            // Loop through each array element, get JSON object which has userid and username
            for (int i = 0; i < arr.length(); i++) {
                // Get JSON object
                JSONObject obj = (JSONObject) arr.get(i);
                System.out.println(obj.get("web_id"));
                System.out.println(obj.get("phone_id"));
                System.out.println(obj.get("msg_id"));
                mydb.updateWebSync(obj.get(obj.get("phone_id").toString(), obj.get("msg_id").toString(), obj.get("web_id").toString());

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", obj.get(mydb.WEB_ID).toString());
                map.put("p_id", obj.get(mydb.COLUMN_ID).toString());
                map.put("s", "1");
                syncL.add(map);
            }
            updateMySQLSyncSts(gson.toJson(syncL), "syncsts");
            Toast.makeText(getApplicationContext(), "Download Messages success!", Toast.LENGTH_SHORT).show();
        }
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "Download Messages error!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

在我的 android sqlite 数据库中,mydb.WEB_ID 的值存储为整数。任何帮助表示赞赏。

Hashmap<String,String>

它只包含字符串值。所以你必须把它转换成字符串。 使用

toString()

功能它会给你错误。

试试

String.ValueOf(obj.get("web_id"))

它将整数值转换为字符串,您的问题得到解决。

编码愉快。 :P

我发现了我的错误...我在 HashMap 中调用了与 json 变量不同的数据库列名。感谢大家的协助。