Reading from a simple FireBase Database

Reading from a simple FireBase Database



I'm having some issues reading from a Firebase Database.



I have a pretty simple layout



"lot" :
"lot1" : "low",
"lot2" : "low",
"lot3" : "low"




Of course MyAppName is above this all.


FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance().getReference();

// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.

lotMap = (HashMap) dataSnapshot.getValue();
Log.d("[Directions Activity]: ", "Lot1 value ====== " +lotMap.get("lot"));
Iterator it = lotMap.entrySet().iterator();
while (it.hasNext())
Map.Entry pair = (Map.Entry)it.next();
Log.d("[Directions Activity]: ", "iterator " + pair.getKey() + " = " + pair.getValue());
System.out.println();
it.remove(); // avoids a ConcurrentModificationException





Here's what returns from log


D/[Directions Activity]:: Lot1 value ====== null //null obviously
//because key lot1 doesn't exist
D/[Directions Activity]:: lot = lot3=low, lot2=low, lot1=low



So to me, it looks like it's returning the string lot3=low, lot2=low, lot1=low, but I'd like to be able to get an array, with each value if possible.



Is this achievable??




3 Answers
3



There's some tweak in your code. Your


FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance().getReference();



should be write like this,


DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference database = myRef.child("anyValueNameYouSpecifyInConsole");



Those 2 lines should be declare outside onCreate method. The one that you need to specify with addValueEventListener is the 2nd DatabaseReference, not the first one. So, it should looks like this from my example,


database.addValueEventListener (new ValueEventListener)



and it will import method.



If you wish the data to be displayed in a particular TextView, then just findViewById the TextView you wanna use and include it in onDataChange method like so,


String x = dataSnapshot.getValue(String.class);
textViewNameDeclared.setText(x);



And don't forget to change the security rule for reading.





Thanks you! I'll give it a shot!
– Rickybobby
Jun 27 '17 at 0:39





This worked perfectly, thank you!
– Rickybobby
Jun 27 '17 at 1:11



I had the same question and this is what i used. Modifying to fit the question here


private List<String> lotList;

lotList = new ArrayList<>();

DatabaseReference reference= database.getInstance().getReference().child("lot");



Now adding value event listener


reference.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)

Iterable<DataSnapshot> mData = dataSnapshot.getChildren();

for(DataSnapshot d : mData)

String lot_string = d.getValue(String.class);
lotList.add(lot_string);



@Override
public void onCancelled(DatabaseError databaseError)


);



you can use typecast to JSONObject and parse the JSONObject


JSONObject


JSONObject jsonObject= new JSONObject((Map)dataSnapshot.getValue());

JSONObject jsonObj= (JSONObject) jsonObject.get("lot");

for (Object key : jsonObj.keySet())
//based on you key types
String keyStr = (String)key;
Object keyvalue = jsonObj.get(keyStr);

//Print key and value
System.out.println("key: "+ keyStr + " value: " + keyvalue);



if you using java 8 than you use lamada expression.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)