简体   繁体   中英

How To Save Changes made To The Items of A list

My app displays a customed list of videos with the option to download using IntentService. The custom list is displayed on the UI using a recyclerView system. Below is a pic of the list

在此处输入图像描述

When a video is clicked and downloaded, the download icon turns blue as seen in the first child of the list shown in the picture above and when it has not been downloaded it shows the default black download icon. I have strung up some codes to make it work, the challenge is, when the app is restarted, The download icon revert to its default color even when it was previously downloaded. How do I update and save changes made to the color of the download icon so it will reflect when the app is restarted? I understand the SharedPreference class can be used to save changes made to the App, but I don't know how to achieve this. Would appreciate any assist that can be lent to achieve this

Below is my onCreate mtd

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lecture);
    setUpRecyclerView();

And below is where I created the setUpRecyclerView()

 private void setUpRecyclerView() {
    Query query = lectureRef.orderBy("position", Query.Direction.ASCENDING);
    FirestoreRecyclerOptions<LectureClasses> options = new FirestoreRecyclerOptions.Builder<LectureClasses>()
            .setQuery(query, LectureClasses.class).build();
    adapter = new LectureClassesAdapter(options);
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
}

Below is my call of the onViewItemclick @overide method setup in my adapter class

 @Override
public void onViewItemClick(DocumentSnapshot snapshot, int position, View itemView) {
    //init variables
    CircularProgressBar progressBarCir = itemView.findViewById(R.id.progress_circular);
    progressBarCir.setProgressMax(100f);
    ImageView downloadImage = itemView.findViewById(R.id.download);
    PopupMenu popupMenu = new PopupMenu(LectureActivity.this,downloadImage);
    LectureClasses lectureClasses = snapshot.toObject(LectureClasses.class);
    lectureClasses = adapter.getItem(position);
    String titleNow = lectureClasses.getTitle();
    String urlNow = lectureClasses.getUrl();

     class DownloadReceiver extends ResultReceiver { 

        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);
            if (resultCode == TichaDownloadService.UPDATE_PROGRESS) {
                int progress = resultData.getInt("progress"); 
                progressBarCir.setProgress(progress);
                downloadImage.setVisibility(View.GONE);
                progressBarCir.setVisibility(View.VISIBLE);
                Log.i("STATUS","DOWNLOADING>>>");
                if (progress == 100) {
                   progressBarCir.setVisibility(View.GONE);
                   downloadImage.setImageDrawable(getDrawable(R.drawable.download_blue));
                   downloadImage.setVisibility(View.VISIBLE);
                }
            }else  {
                Log.i("STATUS"," NOT DOWNLOADING,BOSS");
            }
        }
    }
 }   

you are can to check the file with below code

 File videoFile = new File("patch_video_file");
    if (videoFile.exists()){
        // visible button blue
    }else {
        // visible button default
    }

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