2017-10-18 8 views
0

ファイアベースからのデータをテキスト表示で表示する方法は?ユーザーが予定を作成し、確認をクリックすると通知が送信され、データベースからの日付と時刻が通知に表示されますが、エラーが発生します。エラーFirebaseから読み取るときに "java.util.HashMap型の値をStringに変換できませんでした"

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

答えて

0

あなたは/Appointmentの下であなたのJSON内のレベル(n9nRQnで始まるキー)を持っています。あなたのコードはこのレベルを処理する必要があります。

一つの方法:あなたのDatabaseReferenceで子供を欠落していたので、これが起こっていた

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

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

エラーは同じですが – Bleach

0

は、この問題を解決するには、次のコードを使用してください。

0

代わりに日付と時刻を保持するオブジェクトを使用できます。例えば用 : -

// 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

firebaseからデータを取得するためにシンプルで簡単になります。

関連する問題