I'm trying to use Firebase Database but I get that exception (in the title). First I wrote under onDataChange: Map map = dataSnapshot.getValue(Map.class);
but then they asked me to use a generictypeindicator, which I did. However, I now get this new exception. How do I fix this?
Here are some important lines of code
//accessing the firebase database
database=FirebaseDatabase.getInstance();
//creates a database references
databaseRef1=database.getReference().child("Ben_Ptolemy");
databaseRef2=database.getReference().child("Ptolemy_Ben");
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageText = messageArea.getText().toString();
if(!messageText.equals("")){
Map<String, String> map = new HashMap<String, String>();
map.put("message", messageText);
map.put("user", "Ben");
databaseRef1.push().setValue(map);
databaseRef2.push().setValue(map);
messageArea.setText("");
}
}
});
databaseRef1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("firebase received", "true");
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator );
String message = map.get("message").toString();
String userName = map.get("user").toString();
if(userName.equals("Ben")){
addMessageBox("You:-\n" + message, 1);
}
else{
addMessageBox("Ptolemy" + ":-\n" + message, 2);
}
}
Ok, finally got this to work. I used addChildEventListener
instead of addValueEventListener
and replaced Map map = dataSnapshot.getValue(Map.class);
with Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
and everything worked.