android - How to Convert Firebase data to Java Object...? -
using firebase library send data server in form message(string, string)
added hashmap<string, message>
example:
firebase fb = new firebase(url); firebase msgref = fb.child("finished"); hashmap<string, message> msglist = new hashmap<>(); message msg = new message(m, n); msglist.put(hashkey, msg); msgref.push().setvalue(msglist);
while receiving data firebase method addvalueeventlistener()
getting string in form
{ key = finished, value = { -js9rn0uttjyigdcv8i1={moosa={message=email, name=kamran}}, -js9vsmv6bnvzozpl2l8={moosa={message=msgs, name=imran}}, -js9wtq8yedwvxqmfczb={moosa={message=samsung, name=samad}}, -js9rarxojpkn4ro2ham={moosa={message=message, name=moosa}}, -js9b6f75lwwbsqqnjz0={moosa={message=qmobile, name=bilal}}, -js9adxt8tlgtguccuxu={moosa={message=last, name=moosa}}} }
how can convert message object.....????
there 2 more way data out of firebase datasnapshot
don't require using map<string, object>
.
first appoach use methods of datasnapshot
traverse children:
ref = new firebase("https://my.firebaseio.com/messages").limittolast(10); ref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot messagesnapshot: datasnapshot.getchildren()) { string name = (string) messagesnapshot.child("name").getvalue(); string message = (string) messagesnapshot.child("message").getvalue(); } } @override public void oncancelled(firebaseerror firebaseerror) { } });
in above snippet use getchildren()
iterable
of messages. use child("name")
each specific child property.
the second approach use built-in json-to-pojo serializer/deserializer. when you're sending message list, message
objects inside serialized json , stored in firebase.
to them out of again, have inverse:
ref = new firebase("https://my.firebaseio.com/messages").limittolast(10); ref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot messagesnapshot: datasnapshot.getchildren()) { message message = messagesnapshot.getvalue(message.class); } } @override public void oncancelled(firebaseerror firebaseerror) { } });
in second snippet, we're still using getchildren()
@ messages, deserialize them json straight message
object.
for simple sample application using last approach, have @ firebase's androidchat sample. shows how efficiently deal list of messages (hint: firebaselistadapter
).
Comments
Post a Comment