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.