1

Good day, I would like to ask if there are any solutions regarding to my problem. So here's my code first.

StudentRepoAdapter.class

package com.example.citeresearchrepository.StudentAdapter;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

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

import com.example.citeresearchrepository.Model.ResearchRepository;
import com.example.citeresearchrepository.R;
import com.example.citeresearchrepository.StudentPackage.ViewFileActivity;

import java.util.ArrayList;

import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class StudentRepoAdapter extends RecyclerView.Adapter<StudentRepoAdapter.StudentViewHolder> {
    Context context;
    ArrayList<ResearchRepository> researchRepositoryArrayList;

    public StudentRepoAdapter(Context c, ArrayList<ResearchRepository> repositories){
        context = c;
        researchRepositoryArrayList = repositories;
    }

    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new StudentViewHolder(LayoutInflater.from(context).inflate(R.layout.studentrepo_row,parent,false));

    }

    @Override
    public void onBindViewHolder(@NonNull final StudentViewHolder holder, final int position) {
        holder.researchtitle_view.setText(researchRepositoryArrayList.get(position).getResearchtitle());
        holder.researchdate_view.setText(researchRepositoryArrayList.get(position).getResearchtimeline());
        holder.researchname_view.setText(researchRepositoryArrayList.get(position).getFilecapture());
        holder.researchdescription_view.setText(researchRepositoryArrayList.get(position).getResearchdescription());
        final String url = researchRepositoryArrayList.get(position).getUrl();
        final String filecapture = researchRepositoryArrayList.get(position).getFilecapture();
        holder.btnpdf_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadFile(holder.researchname_view.getContext(), researchRepositoryArrayList.get(position).getFilecapture(),".pdf",DIRECTORY_DOWNLOADS,researchRepositoryArrayList.get(position).getUrl());
            }
        });
        holder.btnpdf_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(holder.btnpdf_view.getContext(), ViewFileActivity.class);
                intent.putExtra("filename", researchRepositoryArrayList.get(position).getResearchtitle());
                intent.putExtra("FilePDF", researchRepositoryArrayList.get(position).getUrl());

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                holder.btnpdf_view.getContext().startActivity(intent);
            }
        });


    }
    public long 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);

        return downloadmanager.enqueue(request);
    }


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

    class StudentViewHolder extends RecyclerView.ViewHolder{
        public TextView researchtitle_view,researchdate_view,researchname_view,researchdescription_view,researchtitle_url;
        public Button btnpdf_download;
        public Button btnpdf_view;
        public StudentViewHolder(@NonNull View itemView) {
            super(itemView);
            researchtitle_view = itemView.findViewById(R.id.researchtitle_view);
            researchdate_view = itemView.findViewById(R.id.researchdate_view);
            researchname_view = itemView.findViewById(R.id.researchname_view);
            researchdescription_view = itemView.findViewById(R.id.researchdescription_view);
            researchtitle_url = itemView.findViewById(R.id.researchtitle_view);
            btnpdf_download = itemView.findViewById(R.id.btnpdf_download);
            btnpdf_view = itemView.findViewById(R.id.btnpdf_view);

        }
    }



}

So basically in my StudentRepoAdapter.class, it has no issues regarding on fetching data for my recyclerview and as you can also see in onBindViewHolder I am trying to pass the researchtitle and research url to the ViewFileActivity.class and it's working fine but here's the code for the class as well.

ViewFileActivity.class

package com.example.citeresearchrepository.StudentPackage;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.example.citeresearchrepository.R;

import java.net.URLEncoder;

public class ViewFileActivity extends AppCompatActivity {

    WebView pdfView;
    String filename, FilePDF;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_file);

        pdfView = findViewById(R.id.pdfView);
        pdfView.getSettings().setJavaScriptEnabled(true);


        filename = getIntent().getStringExtra("filename");
        FilePDF = getIntent().getStringExtra("FilePDF");

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(filename);
        progressDialog.setMessage("File Opening...");


        pdfView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressDialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressDialog.dismiss();
            }
        });

        String url = "";
        try{
            url = URLEncoder.encode(FilePDF, "UTF-8");

        }catch (Exception e){
            e.getMessage();
        }
        String gooeldoc = "https://docs.google.com/gview?embedded=true&url=" + url;
        String finalUrl = gooeldoc;
        pdfView.loadUrl(finalUrl);
    }

}

So yes, it is already fetching the researchtitle and researchurl and and I can open it already BUT the problem is sometimes it WORKS and sometimes it leaves a blank page when I intenting to ViewFileActivity so basically in order for me to open again the file, I have to go back and forth and press the button and see if it will load the url file again... Btw the URL that I am fetching is a PDF File and the URL is stored in RealTime Database...

PS: I tried and searched here already but none of the solutions worked, unfortunately it's either I understood their solution wrong or the solution that worked for them doesn't works on mine. Thank you!

Kai
  • 105
  • 1
  • 8
  • I solved my problem and I found it here. https://stackoverflow.com/a/56492856/14791879 – Kai Dec 13 '20 at 16:45

0 Answers0