我是android studio的新手,对于我的android应用程序,我使用Firebase。
当我单击按钮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);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int i, @NonNull Foto foto) {
viewHolder.setTvNameF(foto.getNameF());
viewHolder.setTvPhoneF(foto.getPhoneF());
viewHolder.setTvAdressF(foto.getAdressF());
viewHolder.setTvMailF(foto.getEmailF());
viewHolder.setTvNoteF(foto.getNoteF());
viewHolder.btnDelete.setOnClickListener(v -> {
delete();
});
}
};
rvFoto.setAdapter(adapter);
}
private void delete() {
Toast.makeText(FotoActivity.this, "remove", Toast.LENGTH_SHORT).show();
}
private void viewRecyclerViewFoto() {
linearLayoutManager=new LinearLayoutManager(this);
rvFoto.setLayoutManager(linearLayoutManager);
rvFoto.setHasFixedSize(true);
}
}
// adapter class
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
private void setInfoFoto() {
DatabaseReference databaseReference=
FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("foto").push();
Map<String,Object> mapFoto=new HashMap<>();
mapFoto.put("id",databaseReference.getKey());
mapFoto.put("name",etNameF.getText().toString());
mapFoto.put("phone",etPhoneF.getText().toString());
mapFoto.put("adress",etAdressF.getText().toString());
mapFoto.put("email",etMailF.getText().toString());
mapFoto.put("note",etNoteF.getText().toString());
databaseReference.setValue(mapFoto);
}
我想从foto
中删除一项,而不是全部数据库。
答案 0 :(得分:1)
只需在您的删除功能中使用以下内容,这将删除“ phone”节点的值。您将需要知道要删除的用户的uid,否则可以将其替换为数据库节点引用,但这将从所有用户ID中删除电话。基本上,您将收听foto节点,获取所有推送ID,并循环遍历推送ID以删除所需的节点。
声明变量
private static final String TAG = "TestActivity";
private DatabaseReference fbDbRef;
OnCreate
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) {
}
});
}