Reputation: 106
I'm trying to set a method to delete an item from firebase Realtime database permanently and for some reason i keep getting an error which says "getRef" doesn't exist, although a lot of people use the same method to delete items, I've tried to read and try solutions from multiple articles but it's not really working, this is my code for the adapter class where the error is:
public class AdminAdapterCalories extends RecyclerView.Adapter<AdminAdapterCalories.MYViewHolder> {
ArrayList<Calories> nlist;
public AdminAdapterCalories(ArrayList<Calories> nlist){
this.nlist=nlist;
}
@NonNull
@Override
public MYViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.admin_card_calorie,parent,false);
return new MYViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MYViewHolder holder, int position) {
holder.food.setText(nlist.get(position).getName());
holder.calories.setText("Calories : "+nlist.get(position).getCalories());
holder.proteins.setText("Proteins : "+nlist.get(position).getProteins());
Picasso.get().load(nlist.get(position).getImage()).into(holder.imge);
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(holder.imge.getContext());
builder.setTitle("Delete Panel");
builder.setMessage("Are You Sure You Want To Delete?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
FirebaseDatabase.getInstance().getReference().child("Food")
.child(getRef(position).getkey()).removeValue();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
});
}
@Override
public int getItemCount() {
return nlist.size();
}
class MYViewHolder extends RecyclerView.ViewHolder {
TextView food,calories,proteins;
ImageView imge,edit,delete;
public MYViewHolder(@NonNull View itemView) {
super(itemView);
food= itemView.findViewById(R.id.food_card_admn);
calories=itemView.findViewById(R.id.caloiries_admin);
proteins=itemView.findViewById(R.id.proteins_admin);
imge=itemView.findViewById(R.id.profile_image_card_admin);
delete=(ImageView)itemView.findViewById(R.id.IvD);
}
}
}
the error is in this line .child(getRef(position).getkey()).removeValue();
I've also tried to put instead of that child(getitemid(position).removevalue();
The model class calories is:
public class Calories {
private String Calories,Name,Proteins,Image;
public Calories() {
}
public Calories(String calories, String name, String proteins,String image) {
Calories = calories;
Name = name;
Proteins = proteins;
Image=image;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
this.Image = image;
}
public String getCalories() {
return Calories;
}
public void setCalories(String calories) {
Calories = calories;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getProteins() {
return Proteins;
}
public void setProteins(String proteins) {
Proteins = proteins;
}
}
This is what the database looks like this is how I generate the id
Upvotes: 1
Views: 729
Reputation: 1702
//First get key before store data in database.
String key = mDatabaseRef.child("Food").push().getKey();
//store this key in model class for later use.(delete)
yourModelClass.setMyKey(key);
yourModelClass.set(allOtherValues);
//then store in database.
mDatabase.child("Food").child(key).setValue(yourModelClass);
//then delete data using "key".
FirebaseDatabase.getInstance().getReference().child("Food")
.child(yourModelClass.getMyKey()).removeValue();
//getMyKey() is just getter method returns stored key.
Upvotes: 0
Reputation: 40820
You need to store item ids using a member variable in Calories
, and create getter & setter for it.
public class Calories {
private String id;
public String getId() {return id;}
public String setId(String id) {this.id = id;}
//....
Then get this id for the item that you need to deleted when it's selected in the RecyclerView
like:
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String id = nlist.get(position).getId();
FirebaseDatabase.getInstance().getReference().child("Food")
.child(id).removeValue();
}
});
UPDATE:
As you set keys manually on firebase console, so you can get them whenever you grab the list of Food
from firebase using a ValueEventListener
And get the key of each item using getKey()
method:
List<Calories> caloryList = new ArrayList();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
String id = singleSnapshot.getKey();
Calories calory = singleSnapshot.getValue(Calories.class);
calory.setId(id);
caloryList.add(calory);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Food");
ref.addValueEventListener(valueEventListener);
Eventually caloryList
should be passed to the RecyclerView
adapter.
Upvotes: 1