Skip to content
Advertisement

Take only the first hashmap value [closed]

I have a hashmap where I register three photos in firebase. Now I need to get only the first photo from the node to put in the ImageView of the adapter. But it seems to be coming in random. Cadastrar.java

private HashMap<String, Object> fotosSelecionadas = new HashMap<>();
fotosSelecionadas.put(foto.getIdFoto(), foto);
pet.setFotos(fotosSelecionadas);
pet.salvar();

Adapter.java

HashMap<String, Object> urlFotos = pet.getFotos();
Object firstValue = urlFotos.values().iterator().next();
Picasso.get().load(String.valueOf(firstValue)).into(holder.foto);

Realtime Database

fotos
-MiLS77rQlWEEoZKeBfm
idFoto:"-MiLS77rQlWEEoZKeBfm"
urlFoto:"https://firebasestorage.googleapis.com/v0/b/enc..."
-MiLS78o9Nagri_mR7WL
idFoto:"-MiLS78o9Nagri_mR7WL"
urlFoto:"https://firebasestorage.googleapis.com/v0/b/enc..."
-MiLS79mdjV2JGFRWI0O
idFoto:"-MiLS79mdjV2JGFRWI0O"
urlFoto: 
"https://firebasestorage.googleapis.com/v0/b/enc..."

Advertisement

Answer

Try this.

public static void main(String[] args) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("one", "壱");
    map.put("two", "弐");
    map.put("three", "参");
    map.put("four", "四");

    System.out.println("hash map = " + map);

    Object firstValue = map.values().iterator().next();

    System.out.println("first value = " + firstValue);
}

output:

hash map = {four=四, one=壱, two=弐, three=参}
first value = 四
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement