Skip to main content

Firebase 3 (Getting Location)

To access a value in your database, you create a DatabaseReference for that location. Here are three references to locations in your database:
DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("ZONES");
DatabaseReference zone1Ref = zonesRef.child("ZONE_1");
DatabaseReference zone1NameRef = zone1Ref.child("ZNAME");
In this snippet: 
  • zonesRef points to /ZONES
  • zone1Ref points to /ZONES/ZONE_1
  • zone1NameRef points to /ZONES/ZONE_1/ZNAME
https://stackoverflow.com/questions/43293935/how-to-get-child-of-child-value-from-firebase-in-android

getValue();
DataSnapshot.getChildren();

To get value from Database on Button click:

You can use addValueEventListener() to retrieve data when an event is triggered.
In your onClick() method you can write :
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

// In the database whenever any data is changed then the below code snipped is going to be executed.
mDatabase.child("<dbLocationTag>").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                // the changed value is going to be stored into the dataSnapshot
                // to retrieve value from dataSnapshot we write

                String value = dataSnapshot.getValue(String.class);
                mValueView.setText(value);

                // NOTE: if you've multiple childs of same node then you can use Map to display all of them : EG: 
                /*Map<String, String> map = dataSnapshot.getValue(Map.class);
                String val1 = map.get("val1");
                String val2 = map.get("val2");
                String val3 = map.get("val3");
                Log.v("TAG", "Value 1 "+val1);
                Log.v("TAG", "Value 2 "+val2);
                Log.v("TAG", "Value 3 "+val3);
*/
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

                mValueView.setText(firebaseError.toString());

            }
        });
for ValueEventListener which update all data, you need to run through all the snapshot to find matching
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

https://stackoverflow.com/questions/44811526/datasnapshot-has-the-object-but-getvalue-will-return-null

DataSnapshot.getKey();

https://stackoverflow.com/questions/38232140/how-to-get-the-key-from-the-value-in-firebase



Comments

Popular posts from this blog

Error when renaming java class nameAndroid Studio

Do you see these terms,"java.lang.ClassNotFoundException, DexPathList", somewhere along the whole chunk of error message, wherever you rename a file? Simple solution would be to clean build! But do make sure the following are rename accordingly too, 1. class name on Directory 2. class: public class MainActivity extends .... 3. Manifest: <activity android:name=".MainActivity"> 4. XML: tools:context=".MainActivity" https://www.facebook.com/Android-Noob-Reboot-1248769758470969/ https://stackoverflow.com/questions/40493946/refactoring-package-name-breaks-app-with-classnotfoundexception-due-to-not-findi

RecyclerView

Benefit of RecyclerView: 1. Standby the next element for display 2. Saves Viewholder scrolled offscreen. Viewholder does not  need to be created or have its view inflated; instead, the app just updates the view's contents to match the new item it was bound to. 3. Adapter use  RecyclerView.adapter.notifyItemChanged to update changed items by rebinding Main Objects 1. RecyclerView Object: Overall container for UI that you add to layout.   The  RecyclerView  creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra.  As the user scrolls through the list, the  RecyclerView  takes the off-screen views and rebinds them to the data which is scrolling onto the screen. 2. Adapter: manage Viewholder Object (create and bind to data, onBindViewHolder()   ) 3. Viewholder Object: view in the list. Display a single item with a view 4. LayoutManager: add Views to RecyclerView https://developer.android.com/guide/topic