如何实现firebase数据删除功能?

Posted

技术标签:

【中文标题】如何实现firebase数据删除功能?【英文标题】:How to implement firebase data delete function? 【发布时间】:2019-09-18 07:00:01 【问题描述】:

这个聊天应用有 6 种类型的消息(文本我的,文本朋友/Img-mine,Img-friend/Audio-mine,Audio-friend) 这种方法让我在应用程序中实现消息删除功能感到困惑。我尝试了 onItemLongClick 并滑动,但我无法成功。以及如何获取要删除的 Firebase 消息 ID?

如何在这个列表消息适配器上实现删除按钮或滑动功能?

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bphvcg.apho.Fragments.ImageMessageFragment;
import com.bphvcg.apho.Models.Message;
import com.bphvcg.apho.R;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;

public class RecyclerListMessageAdapter extends RecyclerView.Adapter<RecyclerListMessageAdapter.ViewHolder>  
    private List<Message> listMessages;
    private int rMessageMine, rMessageFriend, rImageMessageMine, rImageMessageFriend, rAudioMessageMine, rAudioMessageFriend;
    Context context;
    private String mFileName = "";
    private  MediaPlayer mPlayer=null;
    private String currentAudio = "";
    public static ImageButton btnCurrentPlay = null;
    public static MediaPlayer getCurrentMedia = null;

    private StorageReference storageReference = FirebaseStorage.getInstance().getReference();

    boolean isFinish = false; // kiểm tra audio đã hết chưa

    public RecyclerListMessageAdapter(List<Message> listMessages, Context context, int rMessageMine, int rMessageFriend,
                                      int rImageMessageMine, int rImageMessageFriend, int rAudioMessageMine, int rAudioMesageFriend) 
        this.listMessages = listMessages;
        this.rMessageMine = rMessageMine; // layout tin nhắn của bản thân
        this.rMessageFriend = rMessageFriend; // layout tin nhắn của bạn
        this.rImageMessageMine = rImageMessageMine; // layout tin nhắn ảnh của bản thân
        this.rImageMessageFriend = rImageMessageFriend; // layout tin nhắn ảnh của bạn
        this.rAudioMessageMine = rAudioMessageMine;
        this.rAudioMessageFriend = rAudioMesageFriend;
        this.context = context;
    

    public class ViewHolder extends RecyclerView.ViewHolder 
        TextView textViewMessageMine, textViewMessageTimeMine, textViewMessageFriend, textViewMessageTimeFriend;
        TextView imageViewMessageMineTime, imageViewMessageFriendTime;
        ImageView imageViewMessageMine, imageViewMessageFriend;
        CircleImageView avatarSeen, avatarSeenImage, avatarSeenAudio;

        ImageButton btnPlayAudioMessageMine, btnPlayAudioMessageFriend;
        TextView textViewMessageTimeAudioMine, textViewMessageTimeAudioFriend;

        public ViewHolder(View itemView) 
            super(itemView);
            textViewMessageMine = itemView.findViewById(R.id.textViewMessageMine);
            textViewMessageTimeMine = itemView.findViewById(R.id.textViewMessageTimeMine);
            textViewMessageFriend = itemView.findViewById(R.id.textViewMessageFriend);
            textViewMessageTimeFriend = itemView.findViewById(R.id.textViewMessageTimeFriend);

            imageViewMessageMine = itemView.findViewById(R.id.imageViewMessageMine);
            imageViewMessageFriend = itemView.findViewById(R.id.imageViewMessageFriend);
            imageViewMessageMineTime = itemView.findViewById(R.id.imageViewMessageTimeMine);
            imageViewMessageFriendTime = itemView.findViewById(R.id.imageViewMessageTimeFriend);

            btnPlayAudioMessageMine = itemView.findViewById(R.id.btnPlayAudioMessageMine);
            btnPlayAudioMessageFriend = itemView.findViewById(R.id.btnPlayAudioMessageFriend);

            textViewMessageTimeAudioMine = itemView.findViewById(R.id.textViewAudioMessageTimeMine);
            textViewMessageTimeAudioFriend = itemView.findViewById(R.id.textViewAudioMessageTimeFriend);

            avatarSeen = itemView.findViewById(R.id.avatarSeen);
            avatarSeenImage = itemView.findViewById(R.id.avatarSeenImage);
            avatarSeenAudio = itemView.findViewById(R.id.avatarSeenAudio);

        
    

    @Override
    public int getItemViewType(int position) 
        if (listMessages.isEmpty())
            return 0;
        else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && !listMessages.get(position).isImage() && !listMessages.get(position).isAudio()) // The message itself is text
            return 1;
        else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && !listMessages.get(position).isImage() && !listMessages.get(position).isAudio()) // Your message is text
            return 2;
        else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && listMessages.get(position).isImage()) // Your own message is a picture
            return 3;
        else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && listMessages.get(position).isImage()) //Your message is photo
            return 4;
        else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && listMessages.get(position).isAudio())  // Your message is audio
            return 5;
         else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
                && listMessages.get(position).isAudio())  // Your message is audio news
            return 6;
        
        return 0;
    

    @Override
    public RecyclerListMessageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        // Create your own text message view
        View viewMessageMine = LayoutInflater.from(parent.getContext()).inflate(rMessageMine, parent, false);
        // Create your text message view
        View viewMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rMessageFriend, parent, false);
        // Create your own picture message view
        View viewImageMessageMine = LayoutInflater.from(parent.getContext()).inflate(rImageMessageMine, parent, false);
        // Create your picture message view
        View viewImageMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rImageMessageFriend, parent, false);

        View viewAudioMessageMine = LayoutInflater.from(parent.getContext()).inflate(rAudioMessageMine, parent, false);

        View viewAudioMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rAudioMessageFriend, parent, false);

        ViewHolder viewHolder = null;


        switch (viewType) 
            case 0:
                viewHolder = null;
                break;
            case 1:
                viewHolder = new ViewHolder(viewMessageMine);
                break;
            case 2:
                viewHolder = new ViewHolder(viewMessageFriend);
                break;
            case 3:
                viewHolder = new ViewHolder(viewImageMessageMine);
                break;
            case 4:
                viewHolder = new ViewHolder(viewImageMessageFriend);
                break;
            case 5:
                viewHolder = new ViewHolder(viewAudioMessageMine);
                break;
            case 6:
                viewHolder = new ViewHolder(viewAudioMessageFriend);
                break;

        

        return viewHolder;
    

    @Override
    public void onBindViewHolder(@NonNull final RecyclerListMessageAdapter.ViewHolder holder, int position) 
        if (listMessages.isEmpty()) 
            //holder.textViewMessageNow.setVisibility(View.VISIBLE);
         else 
            final Message msg = listMessages.get(position);
            if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && !msg.isImage() && !msg.isAudio()) 
                holder.textViewMessageMine.setText(msg.getContent());
                holder.textViewMessageTimeMine.setText(msg.getTimeMessage().substring(11, 16));
             else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && !msg.isImage() && !msg.isAudio()) 
                holder.textViewMessageFriend.setText(msg.getContent());
                holder.textViewMessageTimeFriend.setText(msg.getTimeMessage().substring(11, 16));
             else if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isImage()) 
                getImageStorage(holder.imageViewMessageMine, msg.getUidSender(), msg.getUidReceiver(), msg.getContent());
                holder.imageViewMessageMineTime.setText(msg.getTimeMessage().substring(11, 16));
                final String nameImage = msg.getContent();
                holder.imageViewMessageMine.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        FragmentManager fm = ((Activity) context).getFragmentManager();
                        ImageMessageFragment imageMessageFragment = ImageMessageFragment.newInstance(nameImage);
                        imageMessageFragment.show(fm, null);
                    
                );
             else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isImage()) 
                getImageStorage(holder.imageViewMessageFriend, msg.getUidSender(), msg.getUidReceiver(), msg.getContent());
                holder.imageViewMessageFriendTime.setText(msg.getTimeMessage().substring(11, 16));
                final String nameImage = msg.getContent();
                holder.imageViewMessageFriend.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        FragmentManager fm = ((Activity) context).getFragmentManager();
                        ImageMessageFragment imageMessageFragment = ImageMessageFragment.newInstance(nameImage);
                        imageMessageFragment.show(fm, null);
                    
                );
             else if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isAudio())  // tin am thanh cua ban than
                holder.textViewMessageTimeAudioMine.setText(msg.getTimeMessage().substring(11, 16));
                holder.btnPlayAudioMessageMine.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        onPlayPressed(holder.btnPlayAudioMessageMine,msg.getUidSender(),
                                msg.getUidReceiver(),msg.getContent());
                    
                );
             else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isAudio())  // tin am thanh cua ban
                holder.textViewMessageTimeAudioFriend.setText(msg.getTimeMessage().substring(11, 16));
                holder.btnPlayAudioMessageFriend.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        onPlayPressed(holder.btnPlayAudioMessageFriend,msg.getUidSender(),
                                msg.getUidReceiver(),msg.getContent());
                    
                );
            

             //If the message itself and the message location last
            if(msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && (position == (listMessages.size() - 1)))
                if(msg.isLastMessageSeen() && !msg.isAudio() && !msg.isAudio())
                    getAvatarSeen(holder.avatarSeen,listMessages.get(listMessages.size() - 1).getUidReceiver());
                    holder.avatarSeen.setVisibility(View.VISIBLE);
                
                else if(msg.isLastMessageSeen() && msg.isImage())
                    getAvatarSeen(holder.avatarSeenImage,listMessages.get(listMessages.size() - 1).getUidReceiver());
                    holder.avatarSeenImage.setVisibility(View.VISIBLE);
                
                else if(msg.isLastMessageSeen() && msg.isAudio())
                    getAvatarSeen(holder.avatarSeenAudio,listMessages.get(listMessages.size() - 1).getUidReceiver());
                    holder.avatarSeenAudio.setVisibility(View.VISIBLE);
                
            
        
    

【问题讨论】:

如果您遇到问题,最好在发布问题时创建MCVE。您为此问题发布了近 250 行代码。很多人需要解析和尝试在线调试。 【参考方案1】:

你可以使用

storageReference.child(key).remove();

你试过了吗?

【讨论】:

以上是关于如何实现firebase数据删除功能?的主要内容,如果未能解决你的问题,请参考以下文章

从 UI 和 Firebase 实时数据库中删除 CardView 数据?

如何从 Firebase 实时数据库中删除一些数据?

如何从 Firebase (Swift) 中删除一个孩子

如何将 Firebase 身份验证链接到实时数据库?

如何使用 Firebase 获取通知响应数据?

如何使用 firebase / vue.js 实时删除功能