无法从 recyclerview 实时数据库中删除项目 [英] Can't delete item from recyclerview realtime database

查看:27
本文介绍了无法从 recyclerview 实时数据库中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一种方法以从 firebase 实时数据库中永久删除项目,但出于某种原因,我不断收到错误消息,显示getRef";不存在,虽然很多人使用相同的方法删除项目,我已经尝试阅读并尝试多篇文章中的解决方案,但它并没有真正起作用,这是我的适配器类代码,错误是:

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

错误在这一行 .child(getRef(position).getkey()).removeValue();我也试过把 child(getitemid(position).removevalue();

the error is in this line .child(getRef(position).getkey()).removeValue(); I've also tried to put instead of that child(getitemid(position).removevalue();

模型类卡路里为:

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

这是数据库的样子 这就是我生成 id 的方式

This is what the database looks like this is how I generate the id

推荐答案

您需要使用 Calories 中的成员变量来存储 item id,并创建 getter &设置器.

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;}
//....

然后为您在 RecyclerView 中选择时需要删除的项目获取此 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();
    }
});

更新:

当您在 firebase 控制台上手动设置键时,因此您可以在使用 ValueEventListener

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

并使用getKey()方法获取每个item的key:

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

最终 caloryList 应该传递给 RecyclerView 适配器.

Eventually caloryList should be passed to the RecyclerView adapter.

这篇关于无法从 recyclerview 实时数据库中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆