简体   繁体   中英

RecyclerView.Adapter dont update button text

When I download the file, I do not update the percentage of downloaded file and update it when the download is complete...

Used code

 URL url = new URL(link);
                            URLConnection conexion = url.openConnection();
                            conexion.connect();

                            int lenghtOfFile = conexion.getContentLength();
                            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                            InputStream input = new BufferedInputStream(url.openStream());
                            String saveFilePath = "/sdcard/" + tag + ".apk";

                            OutputStream output = new FileOutputStream(UserSetting.FILEROOT);

                            byte data[] = new byte[1024];

                            long total = 0;

                            while ((count = input.read(data)) != -1) {
                                total += count;
                                holder.click.setText((int) ((total * 100) / lenghtOfFile)+"");
                                System.out.println((int) ((total * 100) / lenghtOfFile)+"");
                                output.write(data, 0, count);
                            }

                            output.flush();
                            output.close();
                            input.close();
                            holder.click.setText("نصب");

                        } catch (Exception e) {
                        }

first thing put percentage parameter in your holder

  class MyHolder extends RecyclerView.ViewHolder{
 //....
      int percentage = 0;
//...
 }
//then assign the value to it in when downloading progress changed 


 holder.percentage  (int) ((total * 100) / lenghtOfFile);
// after that set the percentage  as click button text 

holder.click.setText(String.valueOf(percentage));
  // and do the same thing in the beginning of onBind method 
  // thus the percentage still updated and won't be lost on scrolling

Note: your code will cause you a problem, the file will be redownloaded every time you scroll and view shown

the easiest way to fix that is trying to cache files so you won't have to download same data every time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM