how to delete firebase position value using swipe controller - android

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.

Related

Unable to Perform Update Operation a Nested Item in Firebase Database?

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

how to update recyclerview when delete an item from adapter class

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.

Firebase not creating new node

I have one node name users which is populating fine, but I am trying to incorporate a new node events, which I am having trouble with. I have copied exactly what works in the users, but I am clearly doing something wrong. It never goes into the OnCompleteListener. Is there something I am missing?
R.string.dbnode_events ="events"
Events events = new Events();
events.setEvent_key(mEventKey);
events.setEvent_title("");
events.setEvent_date("");
events.setEvent_time("");
events.setEvent_millis("");
events.setEvent_desc("");
events.setEvent_filter("");
events.setGroup_number("");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_events))
.child(mEventKey)
.setValue(events).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(CreateEventActivity.this, "Success", Toast.LENGTH_SHORT);
}
});
More Information/ Example: The top user one creates a node no problem but the events one has yet to create. I hope this might give some more insight.
User user = new User();
user.setName(email.substring(0, email.indexOf("#")));
user.setPhone("1");
user.setProfile_image("");
user.setSecurity_level("1");
user.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
user.setEmail(FirebaseAuth.getInstance().getCurrentUser().getEmail());
user.setStreet_address("");
user.setCity("");
user.setState("");
user.setZip("");
user.setMember_filter("Member");
user.setSmall_group_subscription1("");
user.setSmall_group_subscription2("");
user.setSmall_group_subscription3("");
user.setSmall_group_subscription4("");
user.setSmall_group_subscription5("");
user.setShow_phone("No");
user.setShow_email("Yes");
user.setShow_address("No");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_users))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(task1 -> {
FirebaseAuth.getInstance().signOut();
redirectLoginScreen();
}).addOnFailureListener(e -> {
FirebaseAuth.getInstance().signOut();
redirectLoginScreen();
Toast.makeText(RegisterActivity.this, "Database Problem ",Toast.LENGTH_SHORT);
});
/////////////////////////
String mEventKey = UUID.randomUUID().toString();
Events events = new Events();
events.setEvent_key(mEventKey);
events.setEvent_title("");
events.setEvent_date("");
events.setEvent_time("");
events.setEvent_millis("");
events.setEvent_desc("");
events.setEvent_filter("");
events.setGroup_number("");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_events))
.child(mEventKey)
.setValue(events)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(RegisterActivity.this, "Success", Toast.LENGTH_SHORT);
}
});
This is what i have done, probably it will help :D
When you add a class, you have to create a reference like this :
private DatabaseReference Accounts;
And inside the onCreate :
Accounts = FirebaseDatabase.getInstance().getReference("Accounts");
After that, set ur class. I do this for the user :
currentwithID = new Class_user(uID,uSer,matchFound);
And than set it on the node :
uID is the token given by Google
Accounts.child(uID).setValue(currentwithID);
I have done the same things to every node of my database, and it perfectly works.
Try to do that with this code, and tell me if it works :D

How to remove specific nodes in firebase real time database

how do you remove an object in firebase without removing entire "simnumbers" child ? for example only remove "LAkUUug..."
First, As per your comment you need to get autogenerated key.For that :-
public String keyval;
FirebaseDatabase.getInstance().getReference().child("numbers-guess-...").child("simnumbers").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
// for (DataSnapshot child : dataSnapshot.getChildren()) {
// if we want to get do operation in multiple data then write your code here
// }
keyval = dataSnapshot.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//add code in case you not get proper dat from firebase
}
});
To remove value in firbase you need to use removeValue() and as per my view you should use it with addOnCompleteListener().
Now, add that keyval as a key which you want to remove. show below code:-
FirebaseDatabase.getInstance().getReference()
.child("simnumbers").child(keyval).removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
//enter your code what you want excute after remove value in firebase.
} else {
//enter msg or enter your code which you want to show in case of value is not remove properly or removed failed.
Toast.makeText(this, "Remove Failed", Toast.LENGTH_SHORT).show();
}
}
});
For deleting you have to use removeValue() method. You have to know the key value of the child otherwise u cant do it. lets say somehow you managed to get the key value which node you want to delete. then just write the code .
FirebaseDatabase.getInstance().getReference().child("simnumbers").child("LAkUUug.....").removeValue();

Firestore Query chaining using AND

How to chain the query parameters for Firestore. I want to dynamically add query parameters (along with some common ones). But it does not seem to be working. Is this a firestore limitation?
Chaining in the same line works:
db.collection("MY_COLLECTION")
.whereEqualTo("user.firebaseUserId" , FirebaseAuth.getInstance().getUid())
.whereEqualTo("formId",formId)
.whereEqualTo("user.active","true")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
FirestoreResponse response = new FirestoreResponse();
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
//getting results here - works!
} else {
}
}
});
But trying to add the conditions on the query object reference returns results only based on the 1st condition specified:
Query firebaseQuery = collectionReference. whereEqualTo("user.firebaseUserId" , "myuserId"); //only this condition is applied
firebaseQuery.whereEqualTo("user.active","true");
if(someCondition){
firebaseQuery.whereEqualTo("user.smart","true");
}
firebaseQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
FirestoreResponse response = new FirestoreResponse();
if (task.isSuccessful()) {
//returns results only based on the 1st condition !!
} else {
}
}
});
This is strange since the .whereEqualTo returns a Query object.
I have also tried using CollectionReference.get() - along with adding query before to CollectionReference.
I figured out the issue - I was reusing the same query object "firebaseQuery " and calling whereEqualTo on the same object.
The whereEqualTo needs to be called on the query object from previous step instead of using the first query ref.
Query firebaseQuery1 = db.collection("MY_COLLECTION")
.whereEqualTo("user.firebaseUserId" , "someUserId");
Query firebaseQuery2 = firebaseQuery1.whereEqualTo("formId",formId);
Query firebaseQuery3 = firebaseQuery2.whereEqualTo("user.active","true");
firebaseQuery3.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
FirestoreResponse response = new FirestoreResponse();
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
works now !!
//}
} else {
}
}
});

Categories

Resources