【问题标题】:Same key - Different Value with a HashMap相同的键 - 具有 HashMap 的不同值
【发布时间】:2013-10-16 20:15:00
【问题描述】:

我将从我想要实现的目标开始

意图

该软件在 for 循环中解析 XML 数据。处理数据的 for 循环将持续到 50(因为我得到了 50 个不同的结果)。我最初所做的是,doInBackground-方法解析整个 XML 数据并将其保存到 TextViews 并显示它。但现在我想添加一个闪屏,只要数据加载就会显示。

XML 文件是像任何其他普通 XML 文件一样构建的,所以当我通过 for 循环时,键总是相同的,但值不同。

方法

我已经做的是创建一个多维数组,但不幸的是你不能使用字符串作为索引。这就是地图的用途。这是我的方法

stringArray[i]["source"] = sourceString;

嗯,然后我用地图试了一下。但是 map 的问题是,当新的 key 再次出现时,它只会覆盖之前的 key-value 对。

所以我想我会使用带有字符串集合的 HashMap。我是这样处理的; 首先我创建了 HashMap

public HashMap <String, Collection<String>> hashMap = new HashMap<String, Collection<String>>();

然后我将每个键的数据放入 HashMap 中。

hashMap.put("source"        , new ArrayList<String>());

这就是我在for循环中所做的

hashMap.get("source").add(new String(((Node) sourceList.item(0)).getNodeValue()));

然后,当完成时,onPostExecute-方法开始一个新的意图并传递 hashMap。

protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    i.putExtra("hashMap", hashMap);
    startActivity(i);
    finish();
}

在我的 MainActivity 中,我这样做是为了获取数据

Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("hashMap");
rankingDate = new TextView(this);
rankingDate.setText("RankingDate: " + hashMap.get("rankingDate"));
layout.addView(rankingDate);

但这会导致 ClassCastException : `ArrayList cannot be cast to java.lang.String" in this line

source.setText("source: " + hashMap.get("source"));

我猜这是因为hashMap.get("source") 包含源数据的所有值。所以我试图将所有数据保存在一个字符串数组中。但这没有用,但我不知道为什么。 Eclipse 告诉我Type mismatch: cannot convert from String to String[]

有什么建议吗?我很想解决这个问题。

【问题讨论】:

    标签: java android arrays hashmap


    【解决方案1】:

    你打错了:

    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("hashMap");
    

    应该是:

    HashMap<String, Collection<String>> hashMap = (HashMap<String, Collection<String>>)intent.getSerializableExtra("hashMap");
    

    【讨论】:

    • 您好,感谢您的回答。但是现在我仍然有如何读出的问题。我是否正确理解如果我现在做 Collection source = hashMap.get("source") - 我现在在 source-Collection 中拥有 XML 文件的每个来源?或者什么是读出数据的最佳方式。如果我可以将所有数据保存在一个数组中,那将是完美的,因为我需要按索引读取
    • 我个人会将Collection&lt;String&gt; 替换为List&lt;String&gt;。现在,假设您需要带有键 foo 的第二个元素,然后只需使用:String element = map.get("foo").get(1);
    • 再次感谢您的回答,但如果可以的话,我必须再问您一个问题。当我这样做时,它会告诉我The method get(int) is undefined for the type Collection&lt;String&gt; 那么,我现在必须更改我的 HashMap 吗?或者我将如何获得第一个索引。
    • 这就是为什么我告诉你把Collection&lt;String&gt;改成List&lt;String :)
    • 啊,好吧,我改错了集合。现在它就像一个魅力。谢谢!
    【解决方案2】:

    @Eng.Fouad 的回答是正确的,你的选角有误。

    您可以考虑使用 MultiMap 而不是集合地图:

    http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html

    【讨论】:

      【解决方案3】:

      使用地图列表。 您可以调用 list.get(index).get("source") 稍后获取结果。

      半伪代码:

      List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>
      
      foreach(entry in document)
        map = new HashMap<String,String>();
        foreach(xml in entry)
         map.put(xml,xml.value)
        end
        list.put(index++,map)
      end
      

      【讨论】:

        【解决方案4】:

        您在主要活动中错误地投射哈希图。

        试试这个:

        HashMap<String, Collection<String>> hashMap = (HashMap<String, Collection<String>>)intent.getSerializableExtra("hashMap");
        

        希望这会有所帮助:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-18
          • 2012-05-09
          • 2016-11-23
          • 2017-08-23
          • 1970-01-01
          相关资源
          最近更新 更多