0

I'm working on a app that downloads files, I have recyclerview -> cardview (10-20 items), if you click on one the items the process to download the file begins, and at the same time i add a spinner to let user know that the download is in process but I'm having problem canceling the spinner once the download is complete, because I don't know how to reference back an specific cardview on the recyclerview, do i need to pass the position of the card view, the spinner(progressbar) id ?

any help will be appreciated, thank you!

from my adapter class

@Override
public void onBindViewHolder(@NonNull final FirstLevelViewHolder holder, final int position) {
        ItemsLevel2 currentItem = fItems.get(position);

        final String id             = currentItem.getId();
        final String name           = currentItem.getName(); 

        holder.id.setText(id);
        holder.ame.setText(name);   

        holder.fImageButton .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { 
                holder.img.setVisibility(View.INVISIBLE);
                holder.spinner.setVisibility(View.VISIBLE); 
                downloadFile(name);
            }
        });
    }

Here I have all the code for the download

downloadFile(String name) {

}
Pedro
  • 1,440
  • 2
  • 15
  • 36

2 Answers2

1
    int downloadFile(String name) {
     if (finish) return 1;
    }

    ////////////////////////////////////////
public void onClick(View v) { 
                    holder.spinner.setVisibility(View.VISIBLE); 
                   if( downloadFile(name))==1{
     holder.spinner.setVisibility(!View.VISIBLE); 
    }
                }
Ayoub Benayache
  • 1,046
  • 12
  • 28
  • Add a bit of explanation instead of just adding the code. Right now even I'm unable to understand what you've written. – Vedprakash Wagh Jun 07 '19 at 17:18
  • I'm not sure about this: This code checks for download finish *on click*, hence whenever the user clicks the button again. Instead there should be an *active* notification that the download has finished, so the UI can dispose immediately. – Izruo Jun 07 '19 at 17:20
0

If you are downloading file from server, so you should use AsyncTask and download your file in doInBackground() method, show your progressbar in onPreExecute() and close spinner in onPostExecute().

 public class DownloadFileAsyncTask extends AsyncTask<String,String,String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //show Progressbar
        }

        @Override
        protected String doInBackground(String... strings) {
            //downloadFile()
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //close spinner
        }

    }
Prachi Singh
  • 564
  • 3
  • 8