Java 如何在单击“我的”时删除数据库中的一项;删除“;按钮

Java 如何在单击“我的”时删除数据库中的一项;删除“;按钮,java,android,firebase-realtime-database,Java,Android,Firebase Realtime Database,我是android studio的新手,对于我的android应用程序,我使用Firebase。 当我点击我的按钮delete(btnDelete)时,我想从我的数据库中删除一个项目 String uid=FirebaseAuth.getInstance().getCurrentUser().getUid(); Query Query=FirebaseDatabase.getInstance() .getReference().child(“用户”).child(uid).child(“foto

我是android studio的新手,对于我的android应用程序,我使用Firebase。 当我点击我的按钮
delete(btnDelete)
时,我想从我的数据库中删除一个项目

String uid=FirebaseAuth.getInstance().getCurrentUser().getUid();
Query Query=FirebaseDatabase.getInstance()
.getReference().child(“用户”).child(uid).child(“foto”);
公共视图持有者(@NonNull View itemView){
超级(项目视图);
foto_root=itemviewbyd(R.id.foto_root);
tvNameF=itemView.findviewbyd(R.id.tvNameF);
tvPhoneF=itemView.findViewById(R.id.tvPhoneF);
tvAdressF=itemView.findViewById(R.id.tvAdressF);
tvMailF=itemView.findviewbyd(R.id.tvMailF);
tvNoteF=itemView.findviewbyd(R.id.tvNoteF);
btnDelete=itemView.findviewbyd(R.id.btnDelete);
}
公共void setTvNameF(字符串tvNameFs){
tvNameF.setText(tvNameFs);
}
public void setTvPhoneF(字符串tvPhoneFs){
设置文本(tvPhoneFs);
}
}
/*
进入数据库
*/
私有void fetch(){
FirebaseRecyclerOptions选项=
新建FirebaseRecyclerOptions.Builder().setQuery(查询,快照->新建Foto(
snapshot.child(“id”).getKey(),
snapshot.child(“name”).getValue().toString(),
snapshot.child(“电话”).getValue().toString(),
snapshot.child(“address”).getValue().toString(),
snapshot.child(“电子邮件”).getValue().toString(),
snapshot.child(“note”).getValue().toString()).build();
适配器=新的FirebaseRecyclerAdapter(选项){
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
View=LayoutInflater.from(parent.getContext())
.充气(右布局。foto_项目,父项,假);
返回新的ViewHolder(视图);
}
@凌驾
受保护的无效onBindViewHolder(@NonNull ViewHolder ViewHolder,int i,@NonNull Foto Foto){
setTvNameF(foto.getNameF());
setTvPhoneF(foto.getPhoneF());
viewHolder.setTvAdressF(foto.getaddressf());
setTvMailF(foto.getEmailF());
setTvNoteF(foto.getNoteF());
viewHolder.btnDelete.setOnClickListener(v->{
删除();
});
}
};
rvFoto.setAdapter(适配器);
}
私有void delete(){
Toast.makeText(FotoActivity.this,“remove”,Toast.LENGTH_SHORT.show();
}
私有void viewRecycleServiceWFoto(){
linearLayoutManager=新的linearLayoutManager(此);
rvFoto.setLayoutManager(linearLayoutManager);
rvFoto.setHasFixedSize(真);
}
}    
//适配器类
字符串uid=FirebaseAuth.getInstance().getCurrentUser().getUid();
私有void setInfoFoto(){
数据库引用数据库引用=
FirebaseDatabase.getInstance().getReference().child(“用户”).child(uid).child(“foto”).push();
Map mapFoto=新的HashMap();
mapFoto.put(“id”,databaseReference.getKey());
mapFoto.put(“name”,etNameF.getText().toString());
mapFoto.put(“phone”,etPhoneF.getText().toString());
mapFoto.put(“地址”,etAdressF.getText().toString());
mapFoto.put(“email”,etMailF.getText().toString());
mapFoto.put(“note”,etNoteF.getText().toString());
databaseReference.setValue(mapFoto);
}

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

只需在删除功能中使用以下内容,这将删除“phone”节点的值。您需要知道要删除的用户的uid,否则您可以用数据库节点引用替换它,但这将从所有用户ID中删除phone。基本上,您将监听foto节点,获取所有的push id,并通过push id循环删除所需的节点

声明变量

private static final String TAG = "TestActivity";
private DatabaseReference fbDbRef;
一次创建

final String uid = "youruid";
fbDbRef = FirebaseDatabase.getInstance().getReference().child("users")
         .child(uid).child("foto");
你的职能

private void delete() {

fbDbRef.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                final String pushKey = snapshot.getKey();
                Log.d(TAG, "pushKey: " + pushKey);
                fbDbRef.child(pushKey).child("phone").removeValue();

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

我的问题仍然存在,因为我无法指向正确的文件。我认为问题是因为我使用了push()方法,该方法通过单击生成id。我想指出foto对象的id,以便能够将其删除。我将我的对象附加到firebase。请查看我的更新答案,我不知道您使用的是按键谢谢您的帮助,但我更改了我的名称id,我不使用push,所以现在我的用户需要插入名称,此名称就是id。
private void delete() {

fbDbRef.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                final String pushKey = snapshot.getKey();
                Log.d(TAG, "pushKey: " + pushKey);
                fbDbRef.child(pushKey).child("phone").removeValue();

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}