I'm trying to create a Snapchat clone. I'm stuck at the part where the activity is supposed to display a list of email ids of users who have sent snaps to the current user.In fact the message is not even getting logged into logcat. Here is the code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blank);
list=findViewById(R.id.emails);
id=new ArrayList<>();
ArrayAdapter<String> ada=new ArrayAdapter(this,android.R.layout.simple_list_item_1,id);
list.setAdapter(ada);
try {
FirebaseDatabase.getInstance().getReference().child("users").child(mAuth.getCurrentUser().getUid()).child("snaps").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.i("first", "created");
Log.i("pug", dataSnapshot.child("from").getValue().toString());
}
#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) {
}
});
}
catch (Exception e){
Log.e("error",e.getMessage());
}
}
}
Here is the screenshot of the database
In your database structure, it shows that you are not using the uid, instead you are using a random id generated using the push() method, you need to add data using the uid instead to be able to access it when retrieving.
Related
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
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) {
}
});
as the title, I dont want to attach listener for all child nodes, I just want to attach and remove listener for some of them. how can I do that. As I know, if we want to remove the listener, we have to named it, but in this situation we can not named all of listeners. example for this situation:
ROOT
---1
---A
---B
---C
---More here
---2
---D
---E
---F
---More here
I just want to attach and remove listener for A, C or some other nodes. not all of them. How to do that ?
Update : this is the database and the code I am using, but I just want to attach and remove listener from some specific nodes, not all of them. hope this helps.
GlobalConstants.ROOT_REF.child("USERS")
.orderByChild("active").equalTo("online")
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
// update status here
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
// update status here
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
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
This question already has an answer here:
ArrayList not updating inside onChildAdded function
(1 answer)
Closed 4 years ago.
I want to get the whole value(0~5) from destinations
This is part of json data in realtime database
{"results" : [{
"destin_num" : 6,
"destinations" : [ "asdf", "qwwee", "zzzxcv11", "jkkk", "lkjhhg", "zxcvb" ],
"name" : "asdfasdfff",
"order" : 2,
"required_time" : "6hours" }]}
the value I want to put in the String List , I use ChildEventListener() to add the value into List, but the value can't put into the List after finish the ChildEventListener().There will be nothing in List direction How to get the values?
List<String> direction=new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
DatabaseReference reference_contacts = FirebaseDatabase.getInstance().getReference("results").child("1").child("destinations");
reference_contacts.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data=dataSnapshot.getValue().toString();
direction.add(data);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
for(int i=0;i<direction.size();i++){
Log.e("Get data",direction.get(i));
}
}
It's because data is loaded from Firebase asynchronously. Your for loop printing the values is actually run before onChildAdded ever gets called. It's easiest to see this by adding some logging:
Log.i("Firebase", "Before adding listener");
reference_contacts.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i("Firebase", "In onChildAdded");
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) { }
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
Log.i("Firebase", "After adding listener");
When you run this code it prints:
Before adding listener
After adding listener
In onChildAdded
That is probably not the order you expected. But is completely explains why the direction list is empty when you print it, the data hasn't been loaded yet, so onChildAdded has never been invoked.
The solution is typically to add the code that requires the data into the onChildAdded method:
So:
reference_contacts.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data=dataSnapshot.getValue().toString();
direction.add(data);
Log.i("Got data",direction);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
If you only want to show the data once everything is loaded, you can use a ValueEventListener instead:
reference_contacts.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
String data=dataSnapshot.getValue(String.class);
direction.add(data);
}
for(int i=0;i<direction.size();i++){
Log.e("Get data",direction.get(i));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
Note that this is an incredibly common source of confusion for developers new to Firebase (and asynchronous APIs in general). I highly recommend studying some of the answers to some of these previous questions about the topic:
Setting Singleton property value in Firebase Listener
getContactsFromFirebase() method return an empty list (which also shows how to define a custom interface to allow the code that requires the data to be outside onChildAdded/onDataChange.
can't get values out of ondatachange method
ArrayList not updating inside onChildAdded function