0

I have a Lure class which contains an ArrayList of the LureImage class as shown in the code below. Within my repository i make the Database call to get the LiveData<List<Lure>> and then i execute an AsyncTask to get the List<LureImage> for each Lure. The issue with the code i wrote to do this is that it does not always get the List<LureImage> from the database and the result is that sometimes my RecyclerView shows images and sometimes it does not. (i show screenshots of what i mean below)

public class Lure implements Serializable {

    private static final String TAG = "LURE";

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String _lureName;
    private String _lureNotes;

    @Ignore
    private List<LureImage> lureImages = new ArrayList<>();

    public Lure() {

    }

    @Ignore
    public Lure(String lureName, String lureNotes) {
        this._lureName = lureName;
        this._lureNotes = lureNotes;
    }
}

From my LureRepository class i manage to get the LiveData> from the Database and then i want for each Lure to get their corresponding Image class from the Database. I managed to do this using this method.

public LiveData<List<Lure>> getAllLures() {
        LiveData<List<Lure>> luresLiveData = lureDao.getAllLures();
        luresLiveData = Transformations.map(luresLiveData, new Function<List<Lure>, List<Lure>>() {
            @Override
            public List<Lure> apply(List<Lure> input) {
                for (final Lure lure : input) {
                    new GetLureImagesTask(lureDao).execute(lure);
                }
                return input;
            }
        });
        return luresLiveData;
    }

private static class GetLureImagesTask extends AsyncTask<Lure,Void,Void> {
        private LureDao lureDao;

        public GetLureImagesTask(LureDao lureDao) {
            this.lureDao = lureDao;
        }

        @Override
        protected Void doInBackground(Lure... lures) {
            lures[0].setLureImages(lureDao.getLureImages(lures[0].getId()));
            return null;
 }
    }

Here i get the images and display them in the recycler view Here the images are not retrieved enter image description here

Jitendra A
  • 1,568
  • 10
  • 18
pavlos
  • 547
  • 1
  • 9
  • 23

1 Answers1

0

Get images in adapter.

    class YourAdapter() : RecyclerView.Adapter<Recyclerview.ViewHolder>() {

          override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                   // place your asynctask here
          }
    }

Hope this helps. Let me know

Deval
  • 245
  • 1
  • 9