Create exception in firebase - java

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

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

Firebase get vaule of serial number

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();

How to get specific values from firebase realtime database.Similar to a where clause in sql

Am kind of new to android and firebase but I need to get some values from my firebase realtime database similar to a select stmt in sql(select address,name from All_machines where terminal_id = "terminal_id").
When i did the code below i was getting a null pointer exeception. But I guess am yet to understand the concept.
mDatabase = FirebaseDatabase.getInstance().getReference("All Machines");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
try {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
if (childDataSnapshot.getValue() != null){
try {
if (childDataSnapshot.child("terminal_id").getValue().toString().equals(newTerminal_id)){
String Terminal_address= ""+ childDataSnapshot.child("address").getValue();
String Terminal_name = ""+ childDataSnapshot.child("terminal_name").getValue();
terminal_name.setText(Terminal_name);
terminal_address.setText(Terminal_address);
}
else {
Toast.makeText(getApplicationContext(),"No Entry matching barcode",Toast.LENGTH_LONG).show();
}
//Log.v(TAG,""+ childDataSnapshot.child("terminal_name").getValue()); //gives the value for given keyname
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(),"It's null.",Toast.LENGTH_LONG).show();
}
//Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node
//get the terminal id for each child then check if it matches the scanned barcode
}
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Database Structure:
{
"All Machines": {
"-M-JnXkserObKnDj3iZO": {
"address": "OANDO IDIROKO",
"atmClass": "6627",
"bankName": "SPL",
"brand": "NCR",
"id": "-M-JnXkserObKnDj3iZO",
"ip": "1033944",
"latitude": "10333",
"longitude": "10333",
"serial_no": "13528891",
"terminal_Id": "10332368",
"terminal_name": "ATM9"
}
}
}
You can use orderByChild and equalTo methods to filter data in server side like below:
// Get DatabaseReference for All Machines
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("All Machines");
// Use orderByChild to order data by terminal_Id and use equalTo for filter by newTerminal_id
Query query = databaseReference.orderByChild("terminal_Id").equalTo(newTerminal_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String Terminal_address= childSnapshot.child("address").getValue(String.class);
String Terminal_name = childSnapshot.child("terminal_name").getValue(String.class);
terminal_name.setText(Terminal_name);
terminal_address.setText(Terminal_address);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Output: If terminal_Id equals 10332368, then
Terminal_address = "OANDO IDIROKO";
Terminal_name = "ATM9";

How to retrieve all Uid without currentUser?

I don't know how to retrieve all Uid. This is my data:
this is my snippet code to set the data:
reference = db.getReference("Attendance");
reference.child(id).child(uid).child.("Status").setValue(status);
any help thank you guys
To log all UIDs (and names) from the JSON:
reference = db.getReference("Attendance");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot idSnapshot: dataSnapshot.getChildren()) {
System.out.println(idSnapshot.getKey());
for (DataSnapshot userSnapshot: idSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("Status/xname").getValue(String.class));
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}

Bypass a push id node in firebase

I am getting a null when I try to get data of the child node of a push id. My structure look like this
And this is my code for getting the message data
DatabaseReference mMessageDatabase = FirebaseDatabase.getInstance().getReference().child("private_messages");
mMessageDatabase.child(User1).child(User2).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
// getting null in message
String message = dataSnapshot.child("message").getValue(String.class);
Log.e(TAG, "CHAT_LOG: " + message);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Is there something wrong in my code or Am I missing a code there?
EDIT:
I try using a query to test if it gives me data. heres my code
messageDatabase.orderByChild("seen").equalTo("false")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String key = datas.getKey();
String seen = dataSnapshot.child(key).child("seen").getValue(String.class);
Log.e(TAG, "CHAT_LOG: Seen= " + seen);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Fortunately it gives me a data. THe only problem is getting the message. I used a query here and I need to give a specific value to it.
Try the following:
mMessageDatabase.child(User1).child(User2).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String message = datas.child("message").getValue(String.class);
Log.e(TAG, "CHAT_LOG: " + message);
}
}
}
Since the snapshot is at User2 then you can loop inside the direct child which is the push id and retrieve the value of child message

Categories