【问题标题】:Hashmap with Intent not working带有 Intent 的 Hashmap 不起作用
【发布时间】:2015-09-26 06:21:56
【问题描述】:

我正在尝试使用 HashMap 将值传递给其他类,但我不确定为什么会得到如此奇怪的结果。我做了一个 system.out.println 并得到 [{Latitude=1.224119, username=borrow, Longitude=103.930041}],[{Latitude=1.224119, username=borrow, Longitude=103.930041}] 重复但不应该是这样因为我有两个不同的坐标。

当我将它传递给另一个类时,它只返回一个坐标而不是两个。

我的代码有什么问题吗?

从 db 中获取坐标并传递给另一个类:

 protected void onPostExecute(JSONObject json) {
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
HashMap<String, String> h1=new HashMap<String, String>();
            if (json != null) {
                Toast.makeText(Borrower_AP.this, json.toString(),
                        Toast.LENGTH_LONG).show();

                try {
                    dataJsonArr = json.getJSONArray("Posts");
                    for (int i = 0; i < dataJsonArr.length(); i++) {
                        JSONObject c = dataJsonArr.getJSONObject(i);
                        Longitude = c.getDouble("longitude");
                        Latitude = c.getDouble("latitude");
                        String s_lat = Latitude.toString();
                        String s_long = Longitude.toString();
                        h1.put("Latitude",s_lat);
                        h1.put("Longitude",s_long);
                        h1.put("username",username);
                        list.add(h1);
 Intent i = new Intent(this, BorrowerMap.class);
                i.putExtra("list", list);
                System.out.print(list);
                startActivity(i);
                        }

取出值:

 ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
    list=(ArrayList<HashMap<String, String>>) getIntent().getExtras().get("list");
    String Latitude=list.get(0).get("Latitude");
    String Longitude=list.get(0).get("Longitude");
    String s_lat = Latitude.toString();
    String s_long = Longitude.toString();
    System.out.println(Latitude);
    System.out.println(Longitude);

【问题讨论】:

    标签: android hashmap


    【解决方案1】:

    如果我遵循您的问题而不是在循环主体之后进行意图调用。因为它只获取一个列表项并在每个循环过程中进行意图调用。

    for (int i = 0; i < dataJsonArr.length(); i++) {
                            JSONObject c = dataJsonArr.getJSONObject(i);
                            Longitude = c.getDouble("longitude");
                            Latitude = c.getDouble("latitude");
                            String s_lat = Latitude.toString();
                            String s_long = Longitude.toString();
                            h1.put("Latitude",s_lat);
                            h1.put("Longitude",s_long);
                            h1.put("username",username);
                            list.add(h1);
                  }
     Intent i = new Intent(this, BorrowerMap.class);
                    i.putExtra("list", list);
                    System.out.print(list);
                    startActivity(i);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 2021-01-21
      • 2019-07-27
      • 2016-11-23
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多