Firebase get vaule of serial number - java

How to get the value of this automatically without writing each one separately.
I tried this
FirebaseDatabase.getInstance().getReference().child("Orutmen").child("episodeNumber");
but it shows null value.
This one works, but I have to write for each field:
FirebaseDatabase.getInstance().getReference().child("Orutmen").child("1").child("episodeNumber");
Here is the code
m2Database = FirebaseDatabase.getInstance().getReference().child("Orutmen");
m2Database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = snapshot.child("episodeNumber").getValue(String.class);
Toast.makeText(EpisodeListModel.this, value, Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});

You can iterate throught the children of DataSnapshot:
m2Database = FirebaseDatabase.getInstance().getReference().child("Orutmen");
m2Database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
value = snapshot.child("episodeNumber").getValue(String.class);
Toast.makeText(EpisodeListModel.this, value, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});

I figured out a way
Just added a list of strings that collects the values from the snapshot
then retrieve the value by position of item clicked >>
List<String> List = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String value = snapshot.getKey();
List.add(value);
}
Toast.makeText(EpisodeListModel.this, List.get(position), Toast.LENGTH_SHORT).show();

Related

Iterate, Check for condition & delete accordingly : Firebase Realtime

I'm using Firebase realtime database. Below is the structure
Problem Statement: Over a list of "Users", I need to delete child who's having coins = 0
So far I'm stuck here
DatabaseReference deleteRef = dbref.child("Users").child("coins");
Query deleteQuery = deleteRef.orderByValue().equalTo("0");
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
child.getRef().setValue(null);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Any help will be appreciated :)
You need to order the list by the child coins like here:
DatabaseReference deleteRef = dbref.child("Users");
Query deleteQuery = deleteRef.orderByChild("coins").equalTo("0");
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
child.getRef().setValue(null);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});

How can I change the value of child in Firebase database?

I want to change the value of child "toggleStatus" under Reference "BetSlip" as shown below. The already set value is "on" so I want such that when I click the button the value of "toggleStatus" is changed to "off"
BetSlipActivity.toggleCollapse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String timeStamp = betSlip.get(position).getTimeStamp();
String toggleStatus = betSlip.get(position).getToggleStatus();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("BetSlip");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot ds: snapshot.getChildren()) {
String timestamp = ""+ ds.child("timeStamp").getValue();
String toggleStatus = ""+ ds.child("toggleStatus").getValue();
if (timeStamp.equals(timestamp) && toggleStatus.equals("on")) {
//set value to off
}
if (timeStamp.equals(timestamp) && toggleStatus.equals("off")) {
//set value to on
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
If you've got the DataSnapshot for a path in the database, it's easy to get the DatabaseReference that you need to update it:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("BetSlip");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot ds: snapshot.getChildren()) {
String timestamp = ""+ ds.child("timeStamp").getValue();
String toggleStatus = ""+ ds.child("toggleStatus").getValue();
if (timeStamp.equals(timestamp) && toggleStatus.equals("on")) {
ds.child("toggleStatus").getRef().setValue("off");
}
if (timeStamp.equals(timestamp) && toggleStatus.equals("off")) {
ds.child("toggleStatus").getRef().setValue("on");
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
Since you're updating the node based on its existing value, strictly speaking you might need to use a transaction for it.

firebase getting exact childs from database

i have problem to get exact childs from database and add it to the list of "teams". is that possible? i can't find proper way to solve this problem. please help me.
image of my actual database structure
i want to get only values of "2" and "3", just list of all teams without members. As a result of my solution im getting all elements from all children.
i have tried sth like this:
mDatabase.child("teams").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
teamList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
teamList.add(snapshot.getValue().toString());
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Use this
mDatabase.child("teams").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
teamList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
teamList.add(snapshot.getKey());
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Try this:
mDatabase = _firebase.getReference("teams");
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot _dataSnapshot) {
teamList = new ArrayList < > ();
try {
GenericTypeIndicator < HashMap < String, Object >> _ind = new GenericTypeIndicator < HashMap < String, Object >> () {};
for (DataSnapshot _data: _dataSnapshot.getChildren()) {
HashMap < String, Object > _map = _data.getValue(_ind);
teamList.add(_map);
}
} catch (Exception _e) {
_e.printStackTrace();
}
}
#Override
public void onCancelled(DatabaseError _databaseError) { }
});

How to retrieve this data from firebase database

I want to get to a listview the data on "Categorias" folder, but i try everthing and i can't do this.
Fragment code:
myRef.addValueEventListener(new ValueEventListener() {
public static final String TAG = "TNW";
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();
list3 values = td.values();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getActivity().getApplicationContext(), "Ese usuario ya existe ", Toast.LENGTH_SHORT).show();
}
});
Its very simple, here is the code to fetch your values.
DatabaseReference db = FirebaseDatabase.getInstance().getReference("people");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
//get the categorias node
DataSnapshot dsCategorias = ds.child("categorias");
//loop inside the categorias node for all children
for(DataSnapshot dbValSnapshot: dsCategorias.getChildren()){
//Assuming all children have only boolean values
//getting the key and the values
String key = dbValSnapshot.getKey();
boolean value = dbValSnapshot.getValue(Boolean.class);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Let me know if you are not able to understand any part of my code. Thanks

Create exception in firebase

I am trying to filter a listing in firebase. I am using equals to fetch a value that is inside, and now I want to make an excess if there is no data with that value.
for (DataSnapshot data : dataSnapshot.getChildren()) {
if (data.child("statusBookmark").getValue().equals("1")) {
final Profile profile2 = data.getValue(Profile.class);
profile.add(profile2);
}
}
This can also be done easily by using orderByChild() as follows:
reference.child("parent").orderByChild("statusBookmark").equalTo("1").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
final Profile profile2 = data.getValue(Profile.class);
profile.add(profile2);
}
else
Log.d("readThis", "no data available for corresponding value");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You can just simply do the following:
for (DataSnapshot data : dataSnapshot.getChildren()) {
if(data.exists()){
if (data.child("statusBookmark").getValue().equals("1")) {
final Profile profile2 = data.getValue(Profile.class);
profile.add(profile2);
}else{
log.d("check", "Data is not equals to 1");
}
} else{
log.d("check", "Data does not exist");
}
}

Categories