everyone, I am working with recycler view in android and I'm getting data from firebase. The problem is when I press the delete button data is deleted from firebase but in the application new list is added with the old list but I want to show only the new list which does not change the position of the recycler view only remove that item. I'm doing this all in the adapter class.
final DatabaseReference referencePost = database.getReference().child("Posts").child(holder.postId);
referencePost.setValue(null).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Toast.makeText(holder.itemView.getContext(), "" + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
After making sure that the deletion process from Firebase is complete, you can add this line:
.
.
//Successfully Deleted
notifyDataSetChanged ();
You can read about it more than this.
Related
Hello Everyone, I am trying to perform a Update Operation in a Nested Firebase Database .
This is the structure of the database:
I am trying to Update the title text which has Deviled Eggs to Deviled Eggs Updated . I am showing the Data in a RecyclerView
The Only thing I have right Now is the name Appetizers and Snacks .
So basically , First I have to Locate the name Appetizers and Snacks , then Move to the Top Node (0 in this case). Then come down to recipes Node , then get the Current Adapter Key and finally Update the Data
here is the Adapter code that i have tried till now:
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Map<String, Object> map = new HashMap<>();
map.put("title", title.getText().toString());
map.put("time", timeReqd.getText().toString());
map.put("servings", servings.getText().toString());
map.put("ingredients", ingredients.getText().toString());
map.put("steps", steps.getText().toString());
map.put("image", image.getText().toString());
String received_data = holder.title.getText().toString();
//received data has value Appetizers and Snacks
FirebaseDatabase.getInstance().getReference()
.child("All Categories")
.child(received_data)
.updateChildren(map)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
dialog.dismiss();
} else {
Toast.makeText(context, "Failed to Update !", Toast.LENGTH_SHORT).show();
}
}
});
I also wrote this Query :
Query query = FirebaseDatabase.getInstance().getReference("All Categories").orderByChild("name").equalTo(received_data);
I know the basics of Update operation but really cant figure out, what should i write here. Please provide Guidance. Thanks
I am trying to add the HashMap "attending" to the .collection("Users").document(user_id) and it seems that .add() does not work with this, so I used .set(), however it erases all the previously stored data on the document(user_id).
How could I just add the fields to the document rather then rewriting the whole document in this case?
attending.put(ID,a);
firebaseFirestore.collection("Events").document(ID).collection("Users").add(userMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
#Override
public void onComplete(#NonNull Task<DocumentReference> task) {
//Toast.makeText(eventRegistration.this, "User Stores", Toast.LENGTH_SHORT).show();
}
});
firebaseFirestore.collection("Users").document(user_id).set(attending).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(eventRegistration.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
}
});
Using add() on a CollectionReference. always creates a new document with a random ID. Calling set() on a DocumentReference will overwrite the document, but you can change that by passing a second SetOptions type argument that says to merge the new data with the existing data - just call SetOptions.merge() to get that object to pass. update() will always just try to update an existing document.
The fact that you're trying to update with a Map doesn't change the way any of these methods work.
All of these operations are also discussed in the documentation.
I have multiple buttons in my android application and when clicked it has to fetch data from Firebase database and the fetched data is added into a list which has to be passed as an argument to another class constructor . But the fetching of data happens in another thread by default since firebase queries are asynchronous . How do I make sure that list has been added with data from other thread before passing it to constructor ?
It's simple use oncompletelistener you can refer this example:
db=FirebaseFirestore.getInstance();
db.collection("Order").document(TableListFragment.tableno)
.update(
"Items", FieldValue.arrayUnion("ABCD"),
"Quantity", FieldValue.arrayUnion("34")
).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.i("onFailure:",e.toString());
}
});
how to delete firebase values using position (swipe controller)
if i delete values using below method,its delete random values,,i want delete selected value only
swipeController = new SwipeController(new SwipeControllerActions() {
#Override
public void onRightClicked(int position) {
firebaseRecyclerAdapter1.notifyItemRemoved(position);
firebaseRecyclerAdapter1.notifyDataSetChanged();
mDataRef = FirebaseDatabase.getInstance().getReference().child("OrderItemsList").child(gg);
mDataRef.child(d).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
FancyToast.makeText(getApplicationContext(), "Removed Item", FancyToast.LENGTH_LONG, FancyToast.ERROR, R.drawable.ic_clear_black_24dp).show();
firebaseRecyclerAdapter1.notifyDataSetChanged();
firebaseRecyclerAdapter1.startListening();
}
}
});
It looks like you're using an adapter from FirebaseUI. In that case, you can get the DatabaseReference for the node at position with:
mDataRef = firebaseRecyclerAdapter1.getRef(position);
And then you can delete the data at that reference with:
mDataRef.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
...
You need to specifiy which item you're trying to delete. For example, in your case, it should be:
mDataRef = FirebaseDatabase.getInstance().getReference().child("OrderItemsList");
mDataRef.child("Egg Rice").removeValue().addOnCompleteListener.. // and the rest..
Here is how it should be without code: OrderItemsList -> Egg Rice child.
Hello everyone I am making an android application with the help of Firebase. The application is working fine but ever since I tried to add the functionality of a favorite button, I am unable to get the bug out of that "add favorite" part of the code.
current_state="not av"
The above part states the current favorite state of the user.
The below code is for "add favorite" part.
Holder.mFavourites.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (current_state.equals("not fav")){
mFav.child(puid).child(kk).child("fav_status").setValue("Added as " +
"fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Holder.mFavourites.setImageDrawable(getContext().getDrawable(R.drawable.ic_star_black));
current_state = "fav";
Toast.makeText(getContext(),"Added to favourites",Toast
.LENGTH_SHORT).show();
}
});
}else if (current_state.equals("fav")){
mFav.child(puid).child(kk).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
Holder.mFavourites.setImageDrawable(getContext().getDrawable(R.drawable.ic_star_border_black_24dp));
current_state = "not fav";
Toast.makeText(getContext(),"Removed from favourites",Toast
.LENGTH_SHORT).show();
}
}
});
}
}
});
I am getting a problem with the above code-
1. Whenever the first time user click on the code. He gets added to favourite and if he click again on the same button then he is removed from the favourites, but if the user after clicking once (i.e. after getting added as favourites)goes on and click on the add to fav button again,then the toast of removed from favorites appears regardless of whether the user is in favorites database or not.
Is current_state a class variable that equals "not fav"? Cause it looks like your if statement is just reading a string and doesnt actually know whats going on in the database. What if you query all the favs into an arraylist and test your if statement against the arraylist