Correct Method To Store Variables On Firebase For Android -
i integrated firebase android app successfully. since have read every single tutorial, doc, tips, info, code examples, , of course, scoured stack overflow, no way accomplish goal here.
i have 2 variables in slot machine game, mtokens , mspins. right declared in mainactivity, reset every time open app. able store them under userid in firebase. however, have no idea how increase or decrease them, when user uses spins, or wins tokens, rather setting variable again. need display both values in textviews, i've tried countless changes in code, , crashes app. either returns null, or not string. , 1 time worked, didn't update on create, once manually called on button press.
so organize:
how manipulate variables based on gameplay(add or subtract) in regards storing firebase.
and
how show stored values live textviews on toolbar throughout app.
and reiterate, yes. i've read every example find, rather applied or not. work google boss, gets me had result asking. going email firebase, suggest asking here others in future.
this how code looks @ moment, i've edited , changed considerable amount of times. have have been closer solution before, , had no idea.
i'm leaving firebase url, structure van seen.
firebase ref = new firebase("https://luckycashslots.firebaseio.com/data/users/" + mainactivity.uid + "/"); firebase tokref = ref.child("tokens"); //tokref.setvalue(mauthdata.getprovider()); //tokens token = new tokens(100); //ref.setvalue(token); ref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { if (datasnapshot.child("tokens").getvalue() != null) { name = (string) datasnapshot.child("tokens").getvalue().tostring(); tokens.settext(name); //tokens.settext(datasnapshot.getvalue().tostring()); // toast.maketext(getapplicationcontext(), name, toast.length_long).show(); // system.out.println(datasnapshot.getvalue()); // string woot = datasnapshot.getvalue().tostring(); // tokens.settext(woot); } } @override public void oncancelled(firebaseerror firebaseerror) { toast.maketext(getapplicationcontext(), "couldnt update token text", toast.length_long).show(); } });
to change existing value in firebase, use transaction.
from firebase documentation on transactions in android:
firebase upvotesref = new firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-jrhthais-jnplxoqivy/upvotes"); upvotesref.runtransaction(new transaction.handler() { @override public transaction.result dotransaction(mutabledata currentdata) { if(currentdata.getvalue() == null) { currentdata.setvalue(1); } else { currentdata.setvalue((long) currentdata.getvalue() + 1); } return transaction.success(currentdata); //we can abort calling transaction.abort() } @override public void oncomplete(firebaseerror firebaseerror, boolean committed, datasnapshot currentdata) { //this method called once results of transaction. } });
please read entire docs, because covers many topics , problems may run into.
Comments
Post a Comment