2017-10-18 2 views
0

Comment afficher les données de firebase en mode texte? Lorsque l'utilisateur fait le rendez-vous et clique sur confirmer alors enverra une notification alors la notification devrait afficher ma date et heure de base de données mais il a l'erreur.erreur « Impossible de convertir la valeur de type java.util.HashMap à cordes » lors de la lecture de Firebase

Notification Page XML

myRef.child("Appointment").addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      TextView mDate = (TextView) findViewById(R.id.tvdate); 
      // TextView mTime = (TextView) findViewById(R.id.tvtime); 
      for(DataSnapshot ds : dataSnapshot.getChildren()) 
      Log.d("TAG",ds.child("Date").getValue(String.class)); 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      Log.w(TAG, "Failed to read value.", error.toException()); 
     } 
    }); 
} 

Répondre

0

Vous avez un niveau dans votre JSON sous /Appointment (le départ clé avec n9nRQn). Votre code doit gérer ce niveau.

Une façon:

myRef.child("Appointment").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { 
     mDate.setText(childSnapshot.getValue(String.class)); 
     mTime.setText(childSnapshot.getValue(String.class)); 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError error) { 
     Log.w(TAG, "Failed to read value.", error.toException()); 
    } 
    }); 
} 
+0

L'erreur est toujours la même – Bleach

0

Pour résoudre ce problème, s'il vous plaît utilisez le code suivant:

myRef.child("Appointment").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     TextView mDate = (TextView) findViewById(R.id.tvdate); 
     TextView mTime = (TextView) findViewById(R.id.tvtime); 

     for (DataSnapshot ds : dataSnapshot.getChildren()) { 
      mDate.setText(ds.child("Date").getValue(String.class)); 
      mTime.setText(ds.child("Time").getValue(String.class)); 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError error) { 
     Log.w(TAG, "Failed to read value.", error.toException()); 
    } 
}); 

Cela se passait parce que vous manquez un enfant dans votre DatabaseReference.

0

Vous pouvez utiliser un objet à la place qui tiendra votre date et heure. Pour exemple: -

// A new comment has been added, add it to the displayed list 

     Comment comment = dataSnapshot.getValue(Comment.class); 

https://firebase.google.com/docs/database/android/lists-of-data

Ce sera simple et facile d'obtenir les données de firebase.