read data from firebase and set the value of textview - android

i faced an issue like user entered enrollment number and verify and read all data from that and set the value to text view
If user enter enrollment 66 then verify that enrollment and read all that data from 66 and set value of text view to marks of subjects.what i need to do if i want to solve Above problem?
I tried:
reff= FirebaseDatabase.getInstance().getReference().child("Marks");
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String mm=dataSnapshot.child("mcad").getValue().toString();
String jm=dataSnapshot.child("java").getValue().toString();
String nm=dataSnapshot.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

First, your Database Reference returns you a list of data so you need to receive your data as a list.
Second, if you want to filter your data base on enrollment then add a query in your database reference.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query query = databaseReference.child("Marks").orderByChild("enrollment").equalTo(66);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
String mm=data.child("mcad").getValue().toString();
String jm=data.child("java").getValue().toString();
String nm=data.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Related

How can I get the value of an item from firebase using uid?

firebase realtime DB
I pass the id of the item to the next activity. I would like to use the id to retrieve the name of the category and set it to a string. How can I do it?
FirebaseDatabase.getInstance().getReference("categories").child("pass your id")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
In onDataChange you can receive the value.

Firebase DataSnapshot on how to display result in text view

I had already done on the data update part and now i would like to extract the data and display it based on user input. I have a edit text column for user to key-in and text view to display the data that i stored in Firebase. May i know what is the best coding to work on this ? I found alot of them are using DataSnapshot but they directly read specific value from android studio while i would like to read the value from user input.[https://i.stack.imgur.com/aYrOF.png]
Edittext key in -> 978
TextView shows = 12
tv_displayIsland=(TextView)findViewById(R.id.tv_displayIsland);
et_scanIsbn=(EditText)findViewById(R.id.et_scanIsbn);
database= FirebaseDatabase.getInstance().getReference();
database.addValueEventListener(new ValueEventListener() {
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
-- need the coding to read the et_scanIsbn and display in tv_displayIsland--
}
}
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
You can retrieve the value of et_scanIsbn by doing the following:
String sbn = et_scanIsbn.getText().toString();
Then you can do:
database.child("Island").orderByChild("isbn").equalTo(sbn).addValueEvent....
Inside the onDataChange you can display the retrieved value inside the TextView:
for(DataSnapshot ds : dataSnapshot.getChildren()){
String island = ds.child("island").getValue(String.class);
tv_displayIsland.setText(island);
This is my code now.
package com.example.scanning;
public class Scan extends AppCompatActivity {
DatabaseReference database;
TextView tv_displayIsland;
EditText et_scanIsbn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
tv_displayIsland = (TextView) findViewById(R.id.tv_displayIsland);
et_scanIsbn = (EditText) findViewById(R.id.et_scanIsbn);
String sbn = et_scanIsbn.getText().toString();
database = FirebaseDatabase.getInstance().getReference();
database.child("Island").orderByChild("isbn").equalTo(sbn).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String island = ds.child("island").getValue(String.class);
tv_displayIsland.setText(island);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}

Search for a value without knowing child

Here is a screenshot of my database
Firebase Database Image
I got the user key ID by writing the following code
userID=Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
I need to search for the child in that key ROLE to check if it's STUDENT or INSTRUCTOR
The problem is all the solutions I've found it online require a parent node to search through , I don't know if this user is a student or instructor , so I can't just write
FirebaseDatabase.getInstance().getReference().child("students")
or
FirebaseDatabase.getInstance().getReference().child("instructors")
I've tried to access the key directly but with no luck
FirebaseDatabase.getInstance().getReference().child(userID);
So, how could this be implemented?
Check for both
dbRef = FirebaseDatabase.getInstance().getReference().child("students");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isStudent = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
and
dbRef = FirebaseDatabase.getInstance().getReference().child("instructors");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isInstructor = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The value of isStudent and isInstructor will help you find, whether the user was a student or an instructor. If user id is present only in either student or instructor then only one of the variables isStudent or isInstructor will be true. If both are false then user does not exist.

Retrieving Data from Firebase with certain ID

I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...
reference = FirebaseDatabase.getInstance().getReference("Users");
Query query = reference.orderByKey().equalTo(task.author_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Structure
You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How can I get some value from my Firebase?

I am trying to solve how to get a value from my DatabaseReference, but doing so gives me the full address when I want to put it in a TextView
This is the reference:
public static DatabaseReference getUsernameRef(String email){
return FirebaseDatabase.getInstance().getReference("nom_usuarios");
These are the values that I want to rescue:
i tried this:
mPosti.setUsername(FirebaseUtils.getUsernameRef(FirebaseUtils.getCurrentUser().getEmail()).toString());
But instead of receiving the value, I get the address of the database.
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("nom_usuaios").getRef();
database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you will the value in datasnapshot
for (DataSnapshot dataSnapshot : dataSnapshot.getChildren()) {
list.add(dataSnapshot.getValue());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources