0

I am building an AsyncTask and I need to pass multiple images to multiple ImageView containers.

I am trying to build the individual findViewById as "img" + position (an int that changes for each image)but my getResources() does not work.

I have tried passing a context to my AsyncTask when I instantiate it but it gives the same error.

Main Activity

while(i<20) {

            for(String imgURL : imagePaths) {

                new ImageDownloader(new WeakReference<AppCompatActivity>(this),i,getApplicationContext()).execute(imgURL,fpath);
                i++;
            }

        }

AsyncTask

public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {

    int position;

    private WeakReference<AppCompatActivity> caller;

    private final Context contextRef;


    public ImageDownloader(WeakReference<AppCompatActivity> caller, Integer position, final Context context) {
        this.caller = caller;
        this.position = position;
        this.contextRef = context;
    }

 @Override
    protected void onPostExecute(Bitmap bitmap) {
        AppCompatActivity activity = caller.get();

        Context context = contextRef;
        if (context != null) {
            int place = context.getResources().getIdentifier("img" + position, "id", context.getPackageName());
            ImageView imageView  = activity.findViewById(place);
            imageView.setImageBitmap(bitmap);
        }

I keep getting compilation failed, error:

cannot find symbol static getResources

Android
  • 1,420
  • 4
  • 13
  • 23
Maria T
  • 9
  • 1
  • 4
  • 1
    Could you add the `import` statements in the class `ImageDownloader` ? – Arnaud Jul 03 '19 at 08:20
  • as the answers show your code is irritating you are having three references to some kind of `Context`. Your `caller` is the right approach with a `WeakReference`, so you should not need `context` and `contextRef`. Nevertheless the error is strange. If `context` is instance of `Context` it should have the method `getResource()`. Does code completion provide this method? – Christian Jul 03 '19 at 08:29
  • @Arnaud I have import static androidx.core.graphics.drawable.IconCompat.getResources; in my ImageDownloader but appears as unused – Maria T Jul 03 '19 at 08:37
  • @MariaT : Try removing this import and see what happens . – Arnaud Jul 03 '19 at 08:38
  • @Christian I was initially using just the caller but since I got this error I tried fixing it by getting the Context as it was suggested by similar solutions I found but still didn't work – Maria T Jul 03 '19 at 08:38
  • Thanks @Arnaud, I am not getting the error anymore after removing the import. My images are still not getting populated but at least the application is running now. You can write it as answer if you want me to upvote – Maria T Jul 03 '19 at 08:43
  • Well the exact cause is still ambiguous to me, if someone can elaborate further , that person should post a full answer. – Arnaud Jul 03 '19 at 09:00

0 Answers0