So I want to know if there is a way to query based on inner child without having to know the parent in Firebase Database? Here is the exact situation:
As shown in the image, I want to query the entire data snapshot where title = "bj", since I have pushed them automatically so Im unaware of the Key (or parent in this case). Im curious if Fb Query has anyway to look into the nested childs directly? TIA
private void fetchPinnedLocationsForSearchbar(String key) {
Query query = MyApplication.database.getReference("/pins/").child("title").orderByKey().equalTo(key);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.e("-->X-DATA-FROM-QUERY", dataSnapshot.toString());
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
final BusinessModel pinItModel = new BusinessModel();
if (((Boolean) dataSnapshot1.child("ifBusiness").getValue())) {
if
BusinessModel pinItModelTest = dataSnapshot1.getValue(BusinessModel.class);
Log.e("->PicCount", pinItModelTest.getPicCount() + "");
//
_data.add(pinItModelTest);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
So I found the answer from another post on StackOverFlow that suggested the solution should be something like:
FirebaseDatabase.getInstance().getReference("pins").orderByChild("title").equalTo("bj").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.e("-->X--ChildAddedQUERY", dataSnapshot.toString()+"---"+s);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Related
In the below code, I'm doing is what I am checking whether EventsRef dataSnapshot exists or not, if dataSnapshot exists = I am getting some values and storing it in a list and passing that list values to recyclerView method ,in that it displays those values, and my problem is if 2 users in online and both of them getting displayed those details , if user1 removes that reference from firebase database by deleting that event, user2 getting error and getting out of the application . I used child event listener as in the below code but it doesn't works as well . and if i use if else condtions inside the child event listener inside on child Added it also doesn't works , So please help me to solve this both issues .Thanks in advance.
EventsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
EventsRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s)
{
EventsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String eventId = ds.child("Eid").getValue(String.class);
String gender = ds.child("Gender").getValue(String.class);
String serveCount = ds.child("Serve").getValue(String.class);
list.add(eventId);
list2.add(gender);
list3.add(serveCount);
}
if (list.size() != 0)
{
for (int i = 0; i < list.size(); i++)
{
RecyclerView(list.get(i), list2.get(i), list3.get(i));
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot)
{
Toast.makeText(WorkerMyEventActivity.this, "Event removed", Toast.LENGTH_SHORT).show();
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
else
{
progressDialog.dismiss();
TextView txt = (TextView) findViewById(R.id.txt);
txt.setVisibility(View.VISIBLE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
This question already has answers here:
How to arrange firebase database data in ascending or descending order?
(2 answers)
Closed 2 years ago.
In Android, I like to retrieve only the last(descending) 2 key-value of each child.
mDatabase = FirebaseDatabase.getInstance().getReference("users");
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
From what I know you can't reverse the order of the dataSnapshot.getChildren() as mentioned in Firebase Database loop through children starting from last child
But for your particular problem what you can do
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
List<DataSnapshot> list = new ArrayList<>();
for (DataSnapshot dsp: dataSnapshot.getChildren()) {
list.add(snapshot);
}
// It reverses the order of the list.
Collections.reverse(list);
// Now list.get(0) and list.get(1) are the snapshots you need!
}
I want to retrieve data in screenshot nodes in my Firebase Real Time. How can I do that?
This is my database
I have tried this
mDataref.child("Game").orderByChild("ten").equalTo(ten).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
String ScreenURLdata = String.valueOf(dataSnapshot.child("screenshot").getValue());
Game game = new Game();
game.setScreenshotURL(ScreenURLdata);
String ScreenURL = game.getScreenshotURL();
Log.d("scre",ScreenURL);
Model_ScreenShot model_screenShot = new Model_ScreenShot(ScreenURL);
listscreenshot.add(model_screenShot);
adapter = new Adapter_ScreenShot(listscreenshot, getApplicationContext());
viewPager.setPadding(-20, -15, 150, 0);
viewPager.setAdapter(adapter);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The logcat showed this : 2019-12-03 11:36:41.443 8144-8144/com.example.gamingshop D/scre: {s1=https://firebasestorage.googleapis.com/v0/b/skullgaming-5dab3.appspot.com/o/Game%2FBorderlands-3-screenshots-78.jpg?alt=media&token=980c6d95-f4a6-4b13-89ba-1ffec3af07d9,
s2=https://firebasestorage.googleapis.com/v0/b/skullgaming-5dab3.appspot.com/o/Game%2Fbl3_06.jpg?alt=media&token=66a8673e-ed2e-4a23-9fff-d6ef10e16a49}
but it does not work when I try to show it to view.
instead of pushing data use custom Id UUID for unique key or use user's id and simply fetch data using ref.child(userid).child("screenshot")
add read write true in storage Firebase rules
I want to display dayVisitors of all the dates. Hence i want to access all the dayVisitors under Noida Sec1/ all dates.Database structure
This is what I've done but it gives null pointer error: Attempt to invoke virtual method 'java.lang.String com.example.thehighbrow.visitormanagement.DayVisitor.getName()' on a null object reference.
If you want to specifically access just the dayVisitors under Noida Sec 1 you can simply implement this using:
final FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference ref = db.getReference("Noida Sec1");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
if(childSnapshot.hasChild("dayVisitor")) {
for (DataSnapshot visitorSnapshot : childSnapshot.child("dayVisitor").getChildren()) {
Visitor visitorObject = visitorSnapshot.getValue(Visitor.class); //or whatever your dayVisitor object is
//now you can access visitorObject with the fields you created and do whatever like add it to an arraylist
}
}
}
} #Override
public void onCancelled(DatabaseError databaseError) {
Log.e("READ FAILED", databaseError.getMessage());
}
});
I'm not sure what your code is doing, however, I do suggest, if possible, you try to format your database layout to be as flat as possible because nesting data in this manner can get very messy and inefficient. Maybe make dayVisitor a field in visitor rather than its own child node.
First of all get the reference to the children of Noida Sec 1 like:
DatabaseReference mNoidaReference = mFirebaseDatabase.getReference().child("Noida Sec1");
Now make a childEventListener for it and loop through it to find the dayVisitor child
ChildEventListener mChildEventListener = new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for(Datasnapshot data: dataSnapshot.child("dayVisitor")){
String dayVisitor = data.getValue();
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
mNoidaReference.addChildEventListener(mChildEventListener);
As i told you in question when some data changes in firebase realtime databese nothing heppens on datachange() method.
I tried every eventlistener child,value and single but none of them worked.Please help me with that problem i am trying to solve it about 5 hours.
public void StatusTextCheck(){
typing_status.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Toast.makeText(MainActivity.this,"Event listener triggered.",Toast.LENGTH_LONG).show();
TypingStatus type = dataSnapshot.getValue(TypingStatus.class);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Here is the reference define:
private DatabaseReference user_dbref,
message_dbref,typing_status_dbref,typing_status;
user_dbref = database.getReference("Users");
message_dbref = database.getReference("Message");
typing_status = database.getReference("Typing_Status");
#PradyumanDixit