0

I uploaded my files to Firebase's Database, and also list it out using FirebaseUI RecyclerView. My problem is: How to get the download URL from the RecyclerView item that I clicked ? I am only able to get the item's position so far.

This is my FirebaseRecyclerAdapter:

mDatabaseReference = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

    FirebaseRecyclerAdapter<Upload, FileViewHolder> adapter = new FirebaseRecyclerAdapter<Upload, FileViewHolder>(Upload.class, R.layout.design_row, FileViewHolder.class, mDatabaseReference){

        @Override
        protected void populateViewHolder(FileViewHolder viewHolder, Upload model, int position){
            viewHolder.setTitle(model.getName());
            viewHolder.setDate(model.getDate());
        }
    };
    mUploadList.setAdapter(adapter);

This is the ViewHolder:

public static class FileViewHolder extends RecyclerView.ViewHolder{
    View mView;

    private final Context context;

    public FileViewHolder(final View itemView){
        super(itemView);
        context = itemView.getContext();

        mView = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int itemPosition = getLayoutPosition();
                Log.d("position", Integer.toString(itemPosition));

                //CODE TO DOWNLOAD MY FILE ?
            }
        });
    }

    public void setTitle(String title){
        TextView post_title = mView.findViewById(R.id.titleText);
        post_title.setText(title);
    }

    public void setDate(String date) {
        TextView post_date = mView.findViewById(R.id.UploadDate);
        post_date.setText("Uploaded: " + date);
    }
}

*** When I was using ListView previously, I was able to download my file from the list using this code, but am unable to implement the same for RecyclerView:

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //getting the upload
             Upload upload = uploadList.get(1);

            //Opening the upload file in browser using upload url
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(upload.getUrl()));
            startActivity(intent);
        }
    });

Hope to have some insights !

EDIT 1: Following @Arthur Attout's suggestion/method, I have managed to get it working by making the following changes:

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int itemPosition = getLayoutPosition();

                //Snackbar.make(itemView, Integer.toString(itemPosition), Snackbar.LENGTH_LONG).show();

                Upload item = adapter.getItem(itemPosition);
                String url = item.getUrl();

                Log.d("URL", url);

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                context.startActivity(intent);
            }
        });

As well as changing the declaration of the FIrebaseAdapter to static:

    //FirebaseUI RecyclerViewAdapter
static FirebaseRecyclerAdapter<Upload, FileViewHolder> adapter;

I have no idea why adding a static works nor how will it affect my code in the future (only 2 weeks of exposure to Android Java), but as of now, it manages to retrieve the corresponding URL from the list and subsequently download to my device. Thanks to all who contributed towards this discussion! Please feel free to comment on how adding a 'static' affects the code.

Loke WaiEu
  • 65
  • 1
  • 6
  • https://stackoverflow.com/a/41629505/2435238 might help – Sujay Nov 22 '17 at 08:21
  • Thanks for replying, I think I have already done at least that much from that link. My specific question is how to use the info that I have (getLayoutPosition) to download the corresponding file from Firebase's Database ? – Loke WaiEu Nov 22 '17 at 08:27

1 Answers1

0

You can go for

@Override
        public void onClick(View view) {

            int itemPosition = getLayoutPosition();
            Log.d("position", Integer.toString(itemPosition));

            YourItemClass item = (YourItemClass)adapter.getItem(itemPosition);
            String url = item.getUrl();
        }

I haven't tried it out, but I've taken a look at the FirebaseRecyclerAdapter class and that seems to work.

EDIT : I just saw your comment above. If you want to download a picture from a URL, you can check out Picasso, which will allow you to do something like

Picasso.with(context).load(url).into(view);
Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
  • Hi ! I will be testing out the code later, and will keep you updated. By the way, I am uploading and downloading PDF files, not pictures, sorry if I did not make myself clear. – Loke WaiEu Nov 22 '17 at 09:12
  • Just tried it. I am getting the error "Non static field 'adapter' cannot be referenced from a static context". – Loke WaiEu Nov 22 '17 at 09:29
  • You have to pass your adapter in the constructor of your ViewHolder – Arthur Attout Nov 22 '17 at 09:31