Retrieve data from the given firebase data Structure - android

I want to access article information but the problem I face is Article array names are some random keys and not like the one given here.

To access the keys inside the Article node, do the following:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Branch").child("Users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String key=datas.child("keys").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here the datasnapshot is Users you then iterate inside the random keys and get the values(keys) that are inside the Articles.

Related

Getting Firebase child key under random key

I want to retrive division and rno from for all key in array and compare those values in array with two other strings. how to do that?
You can get all children. try below code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference();
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
String rno = ds.child("rno").getValue().toString();
String div = ds.child("division").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I don't understand your question. But you can play on those who fulfill the requirements of the child and fulfill the conditions.
DatabaseReference roomRef = dbRef.child("Everybody Room List Messages");
ValueEventListener roomEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dsKey : dataSnapshot.getChildren()) {
for (DataSnapshot dsNick : dsKey.getChildren()) {
if (dsNick.child("nick").getValue().toString().equals(Nick)) {
dsNick.child("image").getRef().setValue("");
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
roomRef.addListenerForSingleValueEvent(roomEventListener);
UPDATE
So, if I understand, you need this code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot item: dataSnapshot.getChildren()) {
String snapDiv = item.child("division").getValue().toString();
String snapRno = item.child("rno").getValue().toString();
if(snapDiv.equals(divi) && snapRno.equals(roll)){
//Here you can do everythings you need because division and rno value are those you are searching.
}
}
}
});
I can't understand completly your question.
You need to retrieve for each child his division and rno value. After that you need to compare them with other variables. But in the session these variables are 2 and fixed or you need to compare with a list of pairs?
After that, assuming that the above question will be clarify, if strings are matches, you need to update the value of sub1, sub2,...,sub5 fields?
Please, give us more informations to understand your situation and give you more support.

How to Read Data From Firebase database

I want to read data from my Firebase database for all questions. For e.g. for question1 I want to store ques,ans and answeredBy into three variables. What I am currently doing is here.
private FirebaseDatabase database=FirebaseDatabase.getInstance();
private DatabaseReference
quizRef=database.getReference("Quiz").child("Questions");
quizRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds:dataSnapshot.getChildren())
{
ModelClass modelClass=new ModelClass();
modelClass.setQues(ds.child("question1").getValue(ModelClass.class).getQues());
modelClass.setAns(ds.child("question1").getValue(ModelClass.class).getAns());
textViewQuestionText.setText(modelClass.getQues());
}
}
#Override
public void onCancelled(DatabaseError error) {
textViewQuestionText.setText("Error occurred");
}
});
Tried and Tested. You dont have to check for keys like question1,question2 etc
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("Quiz/Questions");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
ModelClass modelClass = (ds.getValue(ModelClass.class));
modelClass.getAns();
modelClass.getQes();
// fetch any key value pair
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I recommend to use firebase generated hash keys instead of your own keys like question1,question2 etc
Check this
You can just use the data from your Firebase Database directly using the following code:
DatabaseReference q1Ref = quizRef.child("question1");
q1Ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child("ques").getValue(String.class);
String answer = dataSnapshot.child("ans").getValue(String.class);
int ansBy = dataSnapshot.child("answeredBy").getValue(Integer.class);
// now you can do what you want with the values stored in the variables
}
#Override
public void onCancelled(DatabaseError error) {
textViewQuestionText.setText("Error occurred");
}
});
To read or write data from the database, you need an instance of DatabaseReference:
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
To read data at a path and listen for changes, use the addValueEventListener() or addListenerForSingleValueEvent() method to add a ValueEventListener to a DatabaseReference.
You can use the onDataChange() method to read a static snapshot of the contents at a given path, as they existed at the time of the event.
For more information you can refer to firebase docs

Retrieving Data From Firebase database using android efficiently

I have a Firebase database of the following:
Root Node
User node
an automatically generated key generated by ref.push()
attributes
I am trying to retrieve only one of the attributes of a single child under the user. Here is a screenshot of my database:
Screenshot of my database
If you check the screenshot, i only want to retrieve email (assume i have no other way of getting it). However, in the code, I wouldn't know the child of User that contains the email I want. This is what i am currently doing but it is very inefficient (this code checks if my current user exists, if not, it adds his/her data to the database):
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("User");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
for (DataSnapshot child:children) {
User user1 = child.getValue(User.class);
Log.d("kharas email",user1.getEmail()+"");
if(!user1.getEmail().equals(firebaseAuth.getCurrentUser().getEmail())){
GraphRequest.newMeRequest(token, new GraphRequest.GraphJSONObjectCallback() {
String birthday = "Not Available";
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
User userInfo = new User();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("User");
birthday = object.has("email")+"";
userInfo.setBirthday(birthday);
userInfo.setEmail(firebaseAuth.getCurrentUser().getEmail());
ref.push().setValue(userInfo);
}
}).executeAsync();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I tried to set the child under User to the email but firebase wouldnt let me use some special characters in the key. Any suggestions to make the code more efficient would be appreciated. Or any key suggestions that would make querying easier would be great.
Thanks in advance
Instead of putting random value or auto generated key you can add user's id which is received from fire base
firebaseAuth.getCurrentUser().getUid()
This will be more easy to access and will be more efficient because whenever you get will logged in, you will have this user_id and corresponding to user_id you can get email instead of checking all values in database
The only way to retrieve email if you dont know the random key is this:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("User");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){ for (DataSnapshot child:children) {
String email=datas.child("email").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Or you can use firebase authentication and add the userid under Users instead of push() then retrieve the userid :
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("User");
ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email=dataSnapshot.child("email").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
That way you do not have to loop.

How to retrieve the data's from Firebase database and store it as a string?

i want to retrieve the rates in the firebase database and store it in an String, can anyone suggest me how to take the values alone from the database. i used this code and cannot get any values.i have added my database image for reference
DatabaseReference rootRef = FirebaseDatabase.getInstance().
getReference("fish-market-ukkadam");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String rates = ds.getValue(String.class);
Log.d("TAG", rates);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
rootRef.addListenerForSingleValueEvent(eventListener);
MY DATABASE IMAGE
Replace this:
FirebaseDatabase.getInstance().getReference("fish-market-ukkadam");
With this:
FirebaseDatabase.getInstance().getReference();
Your database project name is "fish-market-ukkadam", but that is not the name of the root node that you can access by name.

Firebase retrieving the child value from realtime database of Firebase

I am new to Firebase and Android. I have stored user authentication details (name, profile image other than email ID) in my Firebase account. I want to retrieve those data (such as names etc.,) to the other part of my app. How can I retrieve my realtime database child values?
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("userID").child("displayName");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String displayName = dataSnapshot.getValue().toString();
System.out.println(displayName );
}
For more:
Read and Write Data on Android - Firebase Documentation
If you want to get the names of all the users, then you can do it like this
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
mRef.child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Log.v("user_name", snapshot.getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources