so have simple array in firebase shown here:
i want able display messages in textview , add textview when new message added array. code display messages shown here:
myfirebaseref.child("messages").child("message").addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot snapshot) { generictypeindicator<list<string>> t = new generictypeindicator<list<string>>() {}; list<string> messages = snapshot.getvalue(t); if( messages == null ) { system.out.println("no messages"); } else { for(int = 0; < messages.size(); i++) { messagedisplay.settext(messagedisplay.gettext() + "\n" + messages.get(i) + "\n"); } } }
this not working expected , getting "failed bounce type" exception on generictypeindicator. idea going wrong?
what looks array you, looks associative array firebase. difference subtle important, because means cannot deserialize json list<string>
. instead if should map<string,string>
.
but following approach lot simpler , better:
public void ondatachange(datasnapshot snapshot) { if (snapshot.getchildrencount() == 0) { system.out.println("no messages"); } else { (datasnapshot messagesnapshot: snapshot.getchildren()) { string message = messagesnapshot.getvalue(string.class); messagedisplay.settext(messagedisplay.gettext() + "\n" + message + "\n"); } } }
Comments
Post a Comment