gpt4 book ai didi

java - 单击 "delete"按钮时如何删除数据库中的一项?

转载 作者:行者123 更新时间:2023-11-29 23:14:04 26 4
gpt4 key购买 nike

我是 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 中删除一个项目而不是所有数据库。

最佳答案

只需在您的删除函数中使用以下代码,这将删除“phone”节点的值。您将需要知道要删除的用户的 uid,否则您可以将其替换为数据库节点引用,但这将从所有用户 ID 中删除电话。基本上,您将监听照片节点,获取所有推送 ID 并循环遍历推送 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) {

}
});

}

关于java - 单击 "delete"按钮时如何删除数据库中的一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55601623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com