【问题标题】:Android Hashmap issueAndroid Hashmap 问题
【发布时间】:2015-10-07 10:52:40
【问题描述】:

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

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 的值存储为整数。任何帮助表示赞赏。

【问题讨论】:

  • 什么样的错误?编译时间还是运行时间?如果你使用 String.valueOf(obj.get("web_id")) 而不是 obj.get(mydb.WEB_ID).toString() 会发生什么?
  • 报错还是一样。关于它是编译时错误还是运行时错误,我真的不知道区别,但错误开始于13704-13704/? W/System.err:

标签: android hashmap android-sqlite


【解决方案1】:
Hashmap<String,String>

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

toString()

运行它会给你错误。

试试

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

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

愉快的编码。 :P

【讨论】:

  • 只需在你的 logcat 中打印你的 (obj.get("web_id")) 并给我那个值。
  • 尝试使用此代码首先检查您从数据库中获得的内容的长度(),如果长度()>0,然后将其转换。所以只需添加 if((obj.get("web_id")).toString().length()>0)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 2021-10-13
    • 2012-05-24
    • 2011-03-03
    • 2015-02-08
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多