使用按钮从Firebase Firestore删除特定文档

时间:2019-07-04 22:55:22

标签: javascript firebase google-cloud-firestore

每个人打招呼, 在我的项目中,我成功地将文件上传到了Firebase存储中,并将捕获的URL保存到了Firestore中,然后可以通过按一下旁边的按钮来单独下载每个文件。 现在我决定创建一个删除按钮,以在按下特定文件时将其删除,该如何实现? 删除按钮现在可以使用了,但是它删除了我必须在javascript中手动添加的文档名称并尝试使用它,我希望删除按钮可以删除文件,而该文件在同一视图中就​​像按钮下载一样。

//my adapater contain the codes of download and delete //

package com.example.boc;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.ArrayList;

import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class MyPdfAdapter extends RecyclerView.Adapter<MyViewHolder> {

    pdfDownloader pdfDownloader;
    ArrayList<DownModel> downModels;
    FirebaseFirestore db;


    public MyPdfAdapter(pdfDownloader pdfDownloader , ArrayList<DownModel> downModels) {
        this.pdfDownloader = pdfDownloader;
        this.downModels = downModels;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        LayoutInflater layoutInflater = LayoutInflater.from(pdfDownloader.getBaseContext());
        View view = layoutInflater.inflate(R.layout.elements, null, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {

        myViewHolder.mName.setText(downModels.get(i).getName());
        myViewHolder.mLink.setText(downModels.get(i).getLink());
        myViewHolder.deletePdfBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletepdf();
            }
        } );

        myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadFile(myViewHolder.mName.getContext(),downModels.get(i).getName(),".pdf",DIRECTORY_DOWNLOADS,downModels.get(i).getLink());
            }
        });


    }
***//here is the delete operation //***
    private void deletepdf() {

        db.collection( "files" ).document().delete()
                .addOnSuccessListener( new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {

                    }
                } );

    }

    public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {

        DownloadManager downloadmanager = (DownloadManager) context.
                getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);

        downloadmanager.enqueue(request);
    }


    @Override
    public int getItemCount() {
        return downModels.size();
    }
}

0 个答案:

没有答案