简体   繁体   中英

How To Change ImageView drawable For A Single RecyclerView Row

I am not particularly sure how to ask this question, so I will just use images to explain

在此处输入图像描述 在此处输入图像描述

In my App, onClick of the download icon calls a download method for the video in the row. When it is completely downloaded, the Icon changes from black to green. A boolean flag is used to save this state in SharedPreference. This saved state is called again in my RecyclerView Adapter so the downloaded state can reflect when the app is relaunched.

THE CHALLENGE IS...

When the app relaunches, instead only the downloaded row icon to show green, the Icon turns green for every row even when they have not been downloaded. below is my code.

class DownloadReceiver extends ResultReceiver { //DownloadReceiver class
                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"); //get the progress
                        //Set the progress to progressBarCir
                        progressBarCir.setProgress(progress);
                        icon_download.setVisibility(View.GONE);
                        progressBarCir.setVisibility(View.VISIBLE);
                        Log.i("STATUS", "DOWNLOADING>>>");
                        if (progress == 100) { // Downloade process is completed
                            isNotDownloaded = false; // Flagging that the video has been downloaded
                            progressBarCir.setVisibility(View.GONE);
                            // Setting the Download Icon to reflect the New color state
                            icon_download.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
                            icon_download.setVisibility(View.VISIBLE);
                            // Saving the boolean flag in SharedPreferece
                            SharedPreferences sharedPreferences = getSharedPreferences("com.example.instagramclone",MODE_PRIVATE);
                            sharedPreferences.edit().putBoolean("isDownloadedState",isNotDownloaded).commit();
                            
                            // Logging of the save state to confirm state is saved.
                            boolean newState= sharedPreferences.getBoolean("isDownloadedState",true);
                            Log.i("STATE XCHANGE", "DOWNLOADED HENCE, "+String.valueOf(newState));

                        }
                    } else {
                        Log.i("STATUS", " NOT DOWNLOADING,BOSS");
                    }
                }
            }

Below is a snippet of Holder section of my Adapter Class

  public LectureClassesHolder(@NonNull final View itemView) {
        super(itemView);
        // Now we ref each custom layout view item using the itemView
        textViewTitle = itemView.findViewById(R.id.subject_topic);
        textViewDescription = itemView.findViewById(R.id.subject_description);
        textViewDuration = itemView.findViewById(R.id.subject_duration);
        imageViewDownload = itemView.findViewById(R.id.download);
        textViewUrl = itemView.findViewById(R.id.url_link);

        SharedPreferences sharedPreferences = itemView.getContext().getSharedPreferences("com.example.instagramclone",Context.MODE_PRIVATE);
        isNotDownloaded = sharedPreferences.getBoolean("isDownloadedState",true);

        if (isNotDownloaded){
            imageViewDownload.setColorFilter(itemView.getContext().getResources().getColor(R.color.black));
            Log.i("DOWNLOAD STATE ","NOT Downloaded State is "+ isNotDownloaded);
        }else {
            imageViewDownload.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
            Log.i("DOWNLOAD STATE ","NOT Downloaded State is "+ isNotDownloaded);
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onItemClick(getSnapshots().getSnapshot(position), position, itemView);
                }
            }
        });

        // Incase e no work
        imageViewDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onViewItemClick(getSnapshots().getSnapshot(position), position, itemView);
                }
            }
        });
    }

I NEED a way to make the change effective for only the row concerned. Would appreciate any assistance on how to achieve this.

The problem is there is of course no association between the individual records and what is stored.The best solution would be to use a database with a table containing atleast a column for the download url and another for status,store the records with the desired status (for example use 0 for not downloaded status and 1 for downloaded status ) and update status column of the row with your url after download then you can query to check the status of the record.however a quick fix to your solution while still using shared prefrence would be to store the links in an array and check against that array see:

 public static String all_records(Context act)
{

    SharedPreferences prefs = act.getSharedPreferences("SHARED_PREFS_NAME", act.MODE_PRIVATE);

    return prefs.getString("all_records", "[]");
 
 
    
}
 public static void add_download(Activity act,String url)
{
    JSONArray ja=new JSONArray();
    try {
    ja=new JSONArray(all_records(act));
        JSONObject jo=new JSONObject();
        jo.put("url",url);
        ja.put(jo);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    SharedPreferences.Editor saver =act.getSharedPreferences("SHARED_PREFS_NAME", act.MODE_PRIVATE).edit();



    saver.putString("all_records",ja.toString());
    saver.commit();


}
public static boolean is_downloaded(Activity act,String url)
{
    JSONArray ja=new JSONArray();
    try {
    ja=new JSONArray(all_records(act));
        for (int i=0;i<ja.length();i++)
        {
            try {
                Log.e("Check ", "" + ja.getJSONObject(i).getString("url") + " Against " + url);
                if (ja.getJSONObject(i).getString("url").equalsIgnoreCase(url)) {
                    return true;
                }
            }catch (Exception ex){}
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }



    return false;
}

in this case you would use it like this to when downloading

protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);
                if (resultCode == TichaDownloadService.UPDATE_PROGRESS) {
                    int progress = resultData.getInt("progress"); //get the progress
                    //Set the progress to progressBarCir
                    progressBarCir.setProgress(progress);
                    icon_download.setVisibility(View.GONE);
                    progressBarCir.setVisibility(View.VISIBLE);
                    Log.i("STATUS", "DOWNLOADING>>>");
                    if (progress == 100) { // Downloade process is completed
                        isNotDownloaded = false; // Flagging that the video has been downloaded
                        progressBarCir.setVisibility(View.GONE);
                        // Setting the Download Icon to reflect the New color state
                        icon_download.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
                        icon_download.setVisibility(View.VISIBLE);
                        // Saving the boolean flag in SharedPreferece
                        add_download(/*your context*/,/*your url*/);

                    }
                } else {
                    Log.i("STATUS", " NOT DOWNLOADING,BOSS");
                }
            }

on your recycle view adapter onbindview or wherever you want to get retrieve the status of whether downloaded call is_downloaded(/*YOUR CONTEXT*/,"YOUR URL");

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