Android Firebase 实时数据库删除其 ID 由 Firebase 命名的子节点



但我不想对最后一个孩子的ID进行硬编码。 在这里,我想获取单击时位于"yaada"下的特定节点的id,然后将其删除。但我不想对最后一个孩子的 ID 进行硬编码。

final DatabaseReference database = FirebaseDatabase.getInstance((.getReference("photos"(.child(photo_id(.child("yaada"(.child("-M7YsqlH_EZdrbFzdg8y"(; database.removeValue((;

检查持有人.删除....

这是我的数据库

以下是我在FirebaseMethods中添加注释的地方.class

public void addNewComment(final String node, final String mediaId, final String comment){
final String commentId = myRef.push().getKey();
final String dateAdded = new SimpleDateFormat("dd-MM-yyyy ",Locale.getDefault()).format(Calendar.getInstance().getTime());
Query query = myRef.child(mActivity.getString(R.string.user_account_settings_node)).child(userID);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String userName = Objects.requireNonNull(dataSnapshot.child(mActivity.getString(R.string.usernameField)).getValue()).toString();
String profileImage = Objects.requireNonNull(dataSnapshot.child(mActivity.getString(R.string.profilePhotoField)).getValue()).toString();
Comment comment_model = new Comment(comment, dateAdded, userName, profileImage, 0);
myRef.child(node).child(mediaId).child(mActivity.getString(R.string.fieldComments))
.child(Objects.requireNonNull(commentId)).setValue(comment_model);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});

这是我的 CommentListAdapter 类;

public class CommentListAdapter extends ArrayAdapter<Comment> {
private Activity mContext;
private int layoutResource;
private long limit=20;
private UtilityInterface utilityInterface;
private FirebaseAuth mAuth;

CommentListAdapter(@NonNull Activity context, int resource, ArrayList<Comment> comments) {
super(context,resource,comments);
mContext = context;
layoutResource = resource;
utilityInterface = (UtilityInterface)mContext;
}
private static class ViewHolder{
ImageView profileImage;
TextView comment;
//TextView commentLike;
TextView dateAdded;
ImageView addLike;
TextView commentReply;
}


@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
mAuth = FirebaseAuth.getInstance();
SharedPreferences prefs = mContext.getSharedPreferences("pass_user_id", MODE_PRIVATE);
final String user_id = prefs.getString("user_id", "");
final String photo_id = prefs.getString("photo_id", "");

if(convertView==null) {
convertView = LayoutInflater.from(mContext).inflate(layoutResource, parent, false);
holder = new ViewHolder();
holder.profileImage = convertView.findViewById(R.id.comment_profile);
holder.comment = convertView.findViewById(R.id.comment);
//holder.commentLike = convertView.findViewById(R.id.commentLike);
holder.dateAdded = convertView.findViewById(R.id.date_added);
holder.addLike = convertView.findViewById(R.id.comment_heart);
holder.commentReply = convertView.findViewById(R.id.commentReply);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}

final Comment commentData = getItem(position);

//Setting profile image
GlideImageLoader.loadImageWithOutTransition(mContext, Objects.requireNonNull(commentData).getProfile_image(),holder.profileImage);
//Setting userName and comment
final String userName = Objects.requireNonNull(commentData).getUser_name();
SpannableStringBuilder str = new SpannableStringBuilder(userName+" "+commentData.getComment());
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, userName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.comment.setText(str);
//Setting date
holder.dateAdded.setText(commentData.getDate_added());
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//                if (user_id.equals(mAuth.getCurrentUser().getUid().toString())){
//
final Intent mediaIntent = mContext.getIntent();
String photo_id = mediaIntent.getStringExtra("photo_id");
String user_id = mediaIntent.getStringExtra("user_id");
FirebaseDatabase fb_db_instance = FirebaseDatabase.getInstance();
final DatabaseReference database = (DatabaseReference) FirebaseDatabase.getInstance().getReference("photos").child(photo_id).child(mContext.getString(R.string.fieldComment));
//                DatabaseReference db_ref_Main = fb_db_instance.getReference(database.push().getKey());
database.removeValue();  
}
});

return  convertView;
}


}

我建议您在yaada下为子节点分配唯一的名称,然后您可以轻松获取该节点的 id 并删除或执行任何您想要的操作。这是您可以做到的。

//Before pushing, assign a unique userId to each child node
String uniqueId= ( hash created using username and date_added);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference rootReference=firebaseDatabase.getReference();
DatabaseReference myRef = rootReference.child("photos").child(photo_id).child("yaada").child(uniqueId);
CommentClass newComment=new CommentClass(comment,commentLikes,date,profileImage,userName);
myRef.setValue(newComment).addOnCompleteListener(new OnCompleteListener<Void>()    {
@Override
public void onComplete(@NonNull Task<Void> task) {
//add your onComplete logic
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//add your onFailure logic
}
});

因此,下次如果要对该节点进行任何更改,只需使用该节点的 id。

uniqueId.removeValue();

如果您想使用 Firebase 提供的 ID,请按以下步骤操作:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference rootReference=firebaseDatabase.getReference();
DatabaseReference myRef = rootReference.child("photos").child(photo_id).child("yaada");
CommentClass newComment=new CommentClass(comment,commentLikes,date,profileImage,userName);
String childId= myRef.push().getKey();
myRef.child(childId).setValue(newComment);

相关内容

  • 没有找到相关文章