Firebase DataSnapshot on how to display result in text view - android

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) {
}
});
}
}

Related

read data from firebase and set the value of textview

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) {
}
});

Jump to Firebase parent Nodes from Child Nodes

Hello all i have a my fire-base structure like this
Firebase Database
I have an array list inside an node in fire-base , I need to loop through this list and jump to each ID to get information i need like the database photo shows.
what I've tried so far
public static void checkIfCurrentUserLiked(final HomeFragment.PostsViewHolder postsViewHolder, String postKey) {
userID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
likesRef = FirebaseDatabase.getInstance().getReference().child("likes");
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
postsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("likes")) {
for (DataSnapshot ds : dataSnapshot.child("likes").getChildren()) {
likesRef.child(Objects.requireNonNull(ds.getValue()).toString())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Like like = snapshot.getValue(Like.class);
if (like != null && like.getLikedUserID().equals(userID)) {
postsViewHolder.likesImage.setImageResource
(R.drawable.ic_item_home_post_like_filled);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
i have no error , just nothing happens , data are not fetched as expected
Change this:
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
into this:
postsRef = FirebaseDatabase.getInstance().getReference().child("posts").child(postKey);
According to your database, you have node posts before the postKey, therefore you need to add it when referencing to a location.

Retrieving nested data from multiple records in a Firebase Database

I'm trying to retrieve nested data from multiple records and display them in a ListView. My data is recorded like so:
I want to display the GroupNumber and the GroupVaccinationDate of all GroupVaccinations recorded in a listView but at the moment the listView remains empty when I run the following code:
listView = (ListView)findViewById(R.id.listItemAllGroupVaccinations);
databaseReference = FirebaseDatabase.getInstance().getReference("groupVaccinations");
groupVaccinations = new ArrayList<>();
protected void onStart(){
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot groupVaccinationSnapshot : dataSnapshot.getChildren()){
GroupVaccination groupVaccination = groupVaccinationSnapshot.getValue(GroupVaccination.class);
groupVaccinations.add(groupVaccination);
}
AllGroupVaccinationList groupVaccinationInfoAdapter = new AllGroupVaccinationList(ActivityAllGroupVaccinations.this, groupVaccinations);
listView.setAdapter(groupVaccinationInfoAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Any help or suggestions would be greatly appreciated!
Gaston's answer will show you all vaccinations in a single group. If instead you want to show all vaccinations in all groups, you can loop over the nested snapshots in your onDataChange:
databaseReference = FirebaseDatabase.getInstance().getReference("groupVaccinations");
groupVaccinations = new ArrayList<>();
AllGroupVaccinationList groupVaccinationInfoAdapter = new AllGroupVaccinationList(ActivityAllGroupVaccinations.this, groupVaccinations);
listView.setAdapter(groupVaccinationInfoAdapter);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot groupVaccinationSnapshot : dataSnapshot.getChildren()){
for(DataSnapshot vaccinationSnapshot : groupVaccinationSnapshot.getChildren()){
GroupVaccination groupVaccination = vaccinationSnapshot.getValue(GroupVaccination.class);
groupVaccinations.add(groupVaccination);
}
}
groupVaccinationInfoAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
I also changed the code to call groupVaccinationInfoAdapter.notifyDataSetChanged(), so that you can keep using the same adapter and update it if needed.
You will need to go one level deeper where your id -LZk5tpB5PXqHVJkj8Vz (GroupID) contains all the sub pushKeys (groupVaccinationID) with data inside.
listView = (ListView)findViewById(R.id.listItemAllGroupVaccinations);
databaseReference = FirebaseDatabase.getInstance().getReference("groupVaccinations").child("-LZk5tpB5PXqHVJkj8Vz");
groupVaccinations = new ArrayList<>();
protected void onStart(){
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot groupVaccinationSnapshot : dataSnapshot.getChildren()){
GroupVaccination groupVaccination = groupVaccinationSnapshot.getValue(GroupVaccination.class);
groupVaccinations.add(groupVaccination);
}
AllGroupVaccinationList groupVaccinationInfoAdapter = new AllGroupVaccinationList(ActivityAllGroupVaccinations.this, groupVaccinations);
listView.setAdapter(groupVaccinationInfoAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
EDIT
The -LZk5tpB5PXqHVJkj8Vz child should be avoided since is hardcoded to show you this example, instead, you should take that child key from where you get your GroupID, doing that you can get as Frank says all the vaccinations from each GroupID.

Pulling data from Firebase into an Android Application

Above is my firebase database I am trying to pull from.
As you can see I have a "table" called "Make". I have succesfully been able to pull this data into a Listview within my app. The code for this is below.
I now need to pull all the data from "VW_Data" table. This table has over 1000 childen named as "0" up to "1000" with the same headings in each such as "Bearing", "Altitude" etc but with different values. I would like to be able to pull "Bearing" value only from ALL 1000 children of "VW_Data" and display in a list.
However i have attempted only to try pull "VW_Data/0/Bearing" multiple ways which have not worked.
Here is my updated code for this class:
public class Graphview extends Activity
{
//Holds the values gathered from firebase
final ArrayList<String> graphlist = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Sets the layout according to the XML file
setContentView(R.layout.activity_graphview);
//XML variable
final ListView listViewG = findViewById(R.id.listviewG);
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("VW_Data");
database.addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot datas : dataSnapshot.getChildren())
{
String bearing = (String) datas.child("Bearing").getValue();
//Add the info retrieved from datasnapshot into the ArrayList
graphlist.add(bearing);
arrayAdapter = new ArrayAdapter<>(Graphview.this, R.layout.itemview, graphlist);
listViewG.setAdapter(arrayAdapter);
//Will refresh app when the data changes in the database
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}//End onCreate()
Try this :
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("VW_Data");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> verticalSnapshotItr = dataSnapshot.getChildren();
if (verticalSnapshotItr != null) {
for (DataSnapshot verticalItemSnapshot : verticalSnapshotItr) {
String altitude = (String) verticalItemSnapshot.child("Altitude").getValue();
// Parse rest keys here
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Try this:
DatabaseReference db = FirebaseDatabase.getInstance()
.getReference()
.child("VW_Data");
db.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String bearing = datas.child("Bearing").getValue().toString();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
snapshot is at VW_Data then use the for loop, you will be able to iterate inside "0" and retrieve the value bearing

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