简体   繁体   中英

Same key - Different Value with a HashMap

I'll start with what I want to achieve

Intention

The software parses XML-Data in a for-loop. The for-loop which handles the data will go until 50 (because I'm getting 50 different results). What I did at first was, that the doInBackground -method parses the whole XML data and saves it into TextViews and displays it. But now I wanted to add a splash-screen that gets displayed as long the data loads.

The XML-File is built up like any other normal XML-file, so when I go through a for-loop the keys are always the same, but the values differ.

Approaches

What I already did was to create an multidimensional array, but unfortunately you can't use Strings as an index. That's what the Map is for. This was my approach

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

Well, then I tried it with a Map. But the problem with the map was, that when the new key came again, it would just overwrite the previous key-value pair.

So I figured out I would use a HashMap with a String Collection. I handled it like this; First I created the HashMap

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

Then I put data in the HashMap for each key.

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

This is what I have done in the for-loop

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

Then, when finished, the onPostExecute -method starts a new intent and passes the hashMap.

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

And in my MainActivity I'm doing this to get the data

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);

But this results in a ClassCastException : `ArrayList cannot be cast to java.lang.String" in this line

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

I guess it's because hashMap.get("source") contains all values of the source data. So I tried to save all the data in a String Array. But this didn't work, but I don't know why. Eclipse tells me that Type mismatch: cannot convert from String to String[]

Any advice? I'm dying to solve this problem.

You got a typo:

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

should be:

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

@Eng.Fouad answer is correct, you have an error in the casting.

You might consider using a MultiMap rather than a map of collections:

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

Use a list of maps. You can call list.get(index).get("source") and get the result later.

semi pseudocode:

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

Your casting your hashmap incorrectly in your main activity.

Try this:

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

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM