is it possible to retrieve all userID from firebase at one time? - firebase

I have a sets of data in firebase database and I wanted to retrieve all the userID, but not its content, just a list of userID will do. Anyone has done it before?

In what platform are you referring to? Assuming you are talking about java you can do something like that:
usersDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(Datasnapshot snap2 : dataSnapshot.getChildren()){
String userId = snap2.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Related

Changing JavaFX nodes in Firebase onDataChange not working

I'm working on a JavaFX admin application that works with Firebase Realtime Database. I've created a ValueEventListener that acquires the data I need in onDataChange. With this data I want to call a call a method that does something with this data. The problem I ran into is that changing JavaFX elements from inside this onDataChange method isn't working at all.
I've tried narrowing the problem down by putting everything in one class and put all the functionality in the onDataChange method, but that wouldn't work either.
What's confusing me the most about this is that reading these elements is working just fine, while changing them doesn't result in anything. It does work when I execute this code from outside onDataChange.
Code in database helper class:
public void startDropsListener() {
dropsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
OverviewController.getInstance().changeNodes();
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Reading from database failed: " + databaseError.getCode());
}
});
}
Method for changing nodes:
public void changeNodes() {
System.out.println(title.getText()); // This does return the node's text.
title.setText("New title"); // Does not update the node's text.
}
UPDATE:
Printing the title after changing seems to be working, so the value appears to update despite it not showing. Here's the instance related code in case that might be the issue:
private static OverviewController instance;
public OverviewController() {
instance = this;
}
public static OverviewController getInstance() {
if(instance == null){
instance = new OverviewController();
}
return instance;
}
Don't make the controller a singleton.
and do like this...
public class OverviewController implements Initializable{
#FXML
public Label label;
#Override
public void initialize(URL location, ResourceBundle bundle){
DatabaseReference dropsRef = FirbaseDatabase.DatabaseReference("Node");
dropsRef.addValueEventListener(valueEventListener);
}
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Plateform.runLater(()->label.setText(dataSnashot.getValue(SomeClass.class).getLabel());
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Reading from database failed: " + databaseError.getCode());
}
});
}

How do I retrieve a list of all children in a Firebase realtime database?

The structure of my Firebase database is shown below. I want to make a list of all the Rooms in Android Studio. In other words, I need an array of [Room1, Room2, ...] for use in a spinner. How can I do this?
What you can do is loop over all the children of your database and add them to an ArrayList and then use an array adapter to display the list.
What I am saying, looks something like this in code:
rootRef.child("Rooms").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
array.add(ds.child("Users").child("UserID").getValue(String.class));
}
ArrayAdapter adapter = new ArrayAdapter(YourActivity.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// Do something for errors too
}
});

Getting the key in firebase to android studio

How do i get the ID of under the Cats
Try the following:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Cats");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The dataSnapshot here is referring to Cats, then you loop inside of the direct children which is the id in this case and retrieve it using getKey()

Cannot resolve getDownloadUrl() Firebase

can anybody help me? how to fix this.. the getdownloadurl() cannot resolve method
final String downloadUrl = task.getResult().getDownloadUrl().toString();
Try to:
storageReference.child("YOUR_CHILD")
.putFile("FILE")
.addOnSuccessListener(new OnSuccessListener() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
taskSnapshot
.getStorage()
.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener() {
#Override
public void onSuccess(Uri uri) {
//Put your result here
}
});
}
}
It is no longer supported if your project is updated to the latest version. I had the same problem earlier, I tried many ways altering the same object. Instead I found out that firebase has changed the docs on their site and not in their Firebase Assistant. So, to save your time follow this link Firebase Upload Docs
and make necessary changes. It'll take 5 mins to change.
If you want to confirm your problem with mine, here is the link My Problem
for me based on the answer from #JPDroid
this is what worked
file_path.putFile(img_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
#Override
public void onSuccess(Object o) {
String download_url = o.toString();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity(), "Error in Upload !", Toast.LENGTH_LONG).show();
}
});

Subscriber only called once in Rx android and Firebase Realtime-Database Implementation

I am new to android development.
Currently I am implementing Firebase Realtime-Database with Rx android.
Here the Rx Android is used to listen to any changes happened in a particular child node inside the Realtime-Database, retrieve a List of Java class object in it and then return the List so that it can be used by another class.
Below are my code snippets.
1) Here is a class that do basic Database operation such as read, write, update and delete, right now I'm only showing the read operation.
public class FirebaseDatabaseLayer {
private DatabaseReference databaseReference = FirebaseDatabase
.getInstance().getReference();
private List<TodoComponentFirebase> todoComponentFirebases = new ArrayList<>();
private FirebaseUser user;
public Observable<List<TodoComponentFirebase>> readModelFirebase() {
return Observable.create(new Observable.OnSubscribe<List<TodoComponentFirebase>>() {
#Override
public void call(final Subscriber<? super List<TodoComponentFirebase>> subscriber) {
user = FirebaseAuth.getInstance().getCurrentUser();
databaseReference.child(user.getUid())
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot todoComponentSnapshot: dataSnapshot.getChildren()) {
TodoComponentFirebase todoComponentFirebase = todoComponentSnapshot.getValue(TodoComponentFirebase.class);
todoComponentFirebases.add(todoComponentFirebase);
}
subscriber.onNext(todoComponentFirebases);
subscriber.onCompleted();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot todoComponentSnapshot: dataSnapshot.getChildren()) {
TodoComponentFirebase todoComponentFirebase = todoComponentSnapshot.getValue(TodoComponentFirebase.class);
todoComponentFirebases.add(todoComponentFirebase);
}
subscriber.onNext(todoComponentFirebases);
subscriber.onCompleted();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
2) Here is class which has a subscriber that listening to the Observable defined in the class in number 1) inside readModelFirebase method
public class Presenter {
private FirebaseDatabaseLayer firebaseDatabaseLayer;
private Subscription readSubscriber = null;
public void readFirebaseModel() {
readSubscriber = firebaseDatabaseLayer
.readModelFirebase()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<TodoComponentFirebase>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext (List<TodoComponentFirebase> todoComponentFirebases) {
mainView.getFirebaseData(todoComponentFirebases);
}
});
}
}
Here the 2) class pass the List of TodoComponentFirebase to the parameter of mainView.getFirebaseData method (I'm not showing concrete implementation of this method here). The idea here is that the mainView.getFirebaseData method will get a List of TodoComponentObject saved in Firebase Database everytime there's change happened in the respective child node.
MainView class is an AppCompatActivity class, the readFirebaseMethod() inside Presenter class is being called in onCreate method of the MainView class, this serve as subscription initialisation.
At the first time initialization of MainView class, the onNext inside readFirebaseModel() is executed, the data being retrieved seamlessly.
However after that there's no execution on that onNext method even though there's changes occur in Child Node which is defined in the 1) class.
I don't understand why the subscription is not working even though there's changes happen in the child node, it only worked the time it is being initialised. Is there anything missed in my Rx usage ?
Thanks
try remove the
subscriber.onCompleted();
I think when you call the onCompleted() subscriber will end up and not called again after any child event on firebase.

Resources