0

I have this problem I can't solve :

I'm getting some data from fire base database. Every thing works fins except when I reach the title column because the title column is a json tree and it have a key values rows so a new logic must be writen here.

What I tried so far
Create an entry for it and save it in hash map inside my ConfigItem class that I created.

I receive the data just fine, but I don't know who to loop through it.

When I reach the third line in the while loop, I gotjava.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry

What I'm missing here?

public class DataMappingConfig implements DataMappingStrategy {

@Override
public Object mapData(HashMap<String, Object> data ) {

    Log.i("key set " , data.keySet().toString() + " key set ") ;
    HashMap<String , Object > mpair  =null ;
    HashMap<String, String> titlepairs = null ;

    for (String key : data.keySet()) {
        if (key.equals(KEY_TITLE)) {

            final Iterator iterator = data.entrySet().iterator();

            while (iterator.hasNext()){
                mpair = new HashMap<String , Object >() ;
                Map.Entry<String, Object> pair = (Map.Entry<String , Object>) data.entrySet() ;
                HashMap<String,HashMap<String , String>> titleMap = (HashMap<String, HashMap<String, String>>) pair.getValue();
                titlepairs = new HashMap<String, String>();;

            }
        }
    }
        ConfigItem conf = new ConfigItem();
        if (data.containsKey(KEY_LOGIN)) {
            conf.setLogin((Boolean) data.get(KEY_LOGIN)); // note the import above
        }
        if (data.containsKey(KEY_ICON)) {
            conf.setIcon((String) data.get(KEY_ICON));
        }
        if (data.containsKey(KEY_MAIN_SCREEN_ORDER)) {
            conf.setNum(safeLongToInt((Long) data.get(KEY_MAIN_SCREEN_ORDER)));
        }
        if (data.containsKey(KEY_MODE)) {
            String modeValue = (String) data.get(KEY_MODE);
            if (modeValue.equals(VALUE_MODE_EMERGENCY)) {
                conf.setEmenrgency(true);
            } else if (modeValue.equals(VALUE_MODE_ROUTINE)) {
                conf.setRoutine(true);
            }
            conf.setMode((String) data.get(KEY_MODE));
        }
        if (data.containsKey(KEY_STATUS)) {
            conf.setStatus((String) data.get(KEY_STATUS));
        }
        if (data.containsKey(KEY_SHOW_DATE)) {
            try {

                conf.setShowDate((Boolean) data.get(KEY_SHOW_DATE));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                Log.i("ERROR", "" + e.getLocalizedMessage());
            }
        }


        Log.i("Data Map config ", "conf item = " + conf.toString() + "\n");


        return conf;
    }

}
2
  • 1
    stackoverflow.com/questions/31832337/… Commented Jun 14, 2017 at 11:01
  • @Wajdi: If your problem has been solved, add an answer and accept that. If you think the question is not useful anymore delete it. Either way: don't remove the question body. Commented Jun 14, 2017 at 13:37

2 Answers 2

0

You are trying to cast a Set to a Map.Entry.

Since your loop's condition is while (iterator.hasNext()), I'm assuming you intended to use iterator.next() inside the loop.

Change:

Map.Entry<String, Object> pair = (Map.Entry<String , Object>) data.entrySet();

to:

Map.Entry<String, Object> pair = (Map.Entry<String , Object>) iterator.next();

Or even better, change:

final Iterator iterator = data.entrySet().iterator();

to:

final Iterator<Map.Entry<String , Object>> iterator = data.entrySet().iterator();

And then you won't need that cast at all:

while (iterator.hasNext()){
    mpair = new HashMap<String , Object >() ;
    Map.Entry<String, Object> pair = iterator.next();
    HashMap<String,HashMap<String , String>> titleMap = (HashMap<String, HashMap<String, String>>) pair.getValue();
    titlepairs = new HashMap<String, String>();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The line

Map.Entry<String, Object> pair = (Map.Entry<String , Object>) data.entrySet() ;

should be

Map.Entry<String, Object> pair = (Map.Entry<String , Object>) iterator.next();

1 Comment

@wajdi, if either is correct then please accept one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.