猿问

当我点击“删除”按钮时,如何删除数据库上的一个项目?

我是安卓工作室的新手,对于我的安卓应用程序,我使用的是火库。我想在单击按钮 时从数据库中删除一个项目。delete(btnDelete)


    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    Query query= FirebaseDatabase.getInstance()

            .getReference().child("users").child(uid).child("foto");




        public ViewHolder(@NonNull View itemView) {

            super(itemView);

            foto_root=itemView.findViewById(R.id.foto_root);

            tvNameF=itemView.findViewById(R.id.tvNameF);

            tvPhoneF=itemView.findViewById(R.id.tvPhoneF);

            tvAdressF=itemView.findViewById(R.id.tvAdressF);

            tvMailF=itemView.findViewById(R.id.tvMailF);

            tvNoteF=itemView.findViewById(R.id.tvNoteF);

            btnDelete=itemView.findViewById(R.id.btnDelete);

        }


        public void setTvNameF(String tvNameFs){

            tvNameF.setText(tvNameFs);

        }

        public void setTvPhoneF(String tvPhoneFs){

            tvPhoneF.setText(tvPhoneFs);

        }


    }


    /*

    get on dataBase

     */

    private void fetch() {

        FirebaseRecyclerOptions<Foto> options=

                new FirebaseRecyclerOptions.Builder<Foto>().setQuery(query, snapshot -> new Foto(

                snapshot.child("id").getKey(),

                snapshot.child("name").getValue().toString(),

                snapshot.child("phone").getValue().toString(),

                snapshot.child("adress").getValue().toString(),

                snapshot.child("email").getValue().toString(),

                snapshot.child("note").getValue().toString())).build();

        adapter = new FirebaseRecyclerAdapter<Foto, FotoActivity.ViewHolder>(options) {


            @NonNull

            @Override

            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())

                        .inflate(R.layout.foto_item,parent,false);

                return new ViewHolder(view);

            }

        };

我想从数据库中删除一个项目,而不是所有数据库。foto


慕哥9229398
浏览 154回答 1
1回答

忽然笑

只需在删除函数中使用以下命令,这将删除“phone”节点的值。您需要知道要删除的用户的 uid,否则您可以将其替换为数据库节点引用,但随后会从所有用户 ID 中删除电话。基本上,您将侦听 foto 节点,获取所有推送 ID 并循环访问推送 ID 以删除所需的节点。声明变量private static final String TAG = "TestActivity";private DatabaseReference fbDbRef;在创建final String uid = "youruid";fbDbRef = FirebaseDatabase.getInstance().getReference().child("users")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.child(uid).child("foto");您的职能private void delete() {fbDbRef.addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot snapshot : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String pushKey = snapshot.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "pushKey: " + pushKey);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fbDbRef.child(pushKey).child("phone").removeValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
随时随地看视频慕课网APP

相关分类

Java
我要回答