Getting the key in firebase to android studio - firebase

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

Related

Firebase image delete

I'm building a gallery app with firebase.
My problem is that when I delete the image, it deletes the document from firebase, but it does not delete it from the screen and the other images are copied on the screen, I could not handle it, I will show you the example below.
THİS GALLERY ADAPTER
#Override
public void onBindViewHolder(#NonNull GalleryHolder holder, int position) {
Picasso.get().load(galleryAdapterArrayList.get(position).downloadUrl).into(holder.imageView);
}
public class GalleryHolder extends RecyclerView.ViewHolder{
public Gallery gallery;
public CheckBox checkBox;
ImageView imageView;
public FirebaseFirestore firebaseFirestore;
public FirebaseUser firebaseUser;
public FirebaseAuth firebaseAuth;
public String Docıd;
public DocumentSnapshot snapshot;
public CollectionReference collectionReference;
public GalleryHolder(#NonNull View itemview) {
super(itemview);
imageView=itemview.findViewById(R.id.gallerLayoutimageview);
firebaseFirestore=FirebaseFirestore.getInstance();
firebaseAuth=FirebaseAuth.getInstance();
firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
itemview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
PopupMenu popupMenu=new PopupMenu(v.getContext(),v);
popupMenu.setGravity(Gravity.END);
popupMenu.getMenu().add(R.string.sil).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
DocumentReference documentReference=firebaseFirestore.collection("Gallery").document(firebaseUser.getUid()).collection("myGallery").document(galleryAdapterArrayList.get(getPosition()).docId);
documentReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
System.out.println("dsadad");
notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(v.getContext(),R.string.notSilinmedi,Toast.LENGTH_SHORT);
}
});
return false;
}
});
popupMenu.show();
return false;
}
});
}
}
}
THİS GALLERY ACTIVITY
public void getData() {
firebaseFirestore.collection("Gallery").document(firebaseUser.getUid()).collection("myGallery").orderBy("date", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot value, #Nullable FirebaseFirestoreException error) {
if (error != null) {
Toast.makeText(getApplicationContext(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
if (value != null) {
for (DocumentSnapshot snapshot : value.getDocuments()) {
Map<String, Object> data = snapshot.getData();
String downloadUrl = (String) data.get("downloadUrl");
String docId = (String) data.get("docId");
checkBox=findViewById(R.id.checkimages);
GalleryModel galleryModel=new GalleryModel(docId,downloadUrl,checkBox);
galleryModelArrayList.add(galleryModel);
}
galleryAdapter.notifyDataSetChanged();
}
}
});
}
before deleting images
after deleting images
Reset activity

how to write a query firebase

I'm trying to filter specific user posts like this:
filter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
ProfileActivity.this, R.style.BottomSheetDialogTheme
);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
final RadioButton b = bottomSheetView.findViewById(R.id.breakfast);
final RadioButton dinner = bottomSheetView.findViewById(R.id.dinner);
if(dinner.isChecked()){
Query reference = FirebaseDatabase.getInstance().getReference("posts").orderByChild("Meal");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.getValue().equals(dinner.getText().toString()))
{
Toast.makeText(ProfileActivity.this, dinner.getText() + "is clicked", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
I want to check whether the checked radioButton value is equal to Meal from database, the current code doesn't show the toast message which means the condition is not met even though it is supposed to. How can I write a proper query for this case?
Query reference = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("Meal");
This will return list of matched data snapshot.
get each snapshot like
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
if (ds.getValue().equals(dinner.getText().toString())) {
Toast.makeText(ProfileActivity.this, dinner.getText() + "is clicked", Toast.LENGTH_SHORT).show();
}
}
}
});

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

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

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

Resources