0

I have a fagment with a Listview which have an ImageView and a Textview inside. So when the fragment creates, I call an AsyncTaskLoader for populating it. In the onLoadFinished method I set the adaptor for the list. The problem is that I cant access getLoadedManager :S

Here is the code: This is the onLoadFinishedMethod from the first Task

public void onLoadFinished(Loader<ArrayList<Entrada>> loader, ArrayList<Entrada> entradas) {
    //progress.setVisibility(View.GONE);
    entradaAdapter adapter = new entradaAdapter(getActivity(), entradas);
    ListView entradas_list = (ListView)rootView.findViewById(R.id.entries_list);
    entradas_list.setAdapter(adapter);
    for (Entrada e : entradas) {
        e.loadImagen(adapter);
    }
}

The adapter:

class entradaAdapter extends ArrayAdapter<Entrada> {
private final ArrayList<Entrada> lista;
LayoutInflater inflater;
public entradaAdapter(Context context, ArrayList<Entrada> lista) {
    super(context, 0, lista);
    this.lista = lista;
    inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    final Entrada e = getItem(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.entrada, null);
    }

    holder = new ViewHolder();
    holder.nombre = (TextView) convertView.findViewById(R.id.text_entryTitle);
    holder.imagen = (ImageView) convertView.findViewById(R.id.imageEntry);

    holder.nombre.setText(e.getNombre());

    if (e.getImage() != null) {
        Drawable d = new BitmapDrawable(getContext().getResources(),e.getImage());
        holder.imagen.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holder.imagen.setImageDrawable(d);
    }
    return convertView;
}

class ViewHolder {
    TextView nombre;
    ImageView imagen;
}

public int getCount() {
    return lista.size();
}

public Entrada getItem(int arg0) {
    return lista.get(arg0);
}

public long getItemId(int position) {
    return position;
}
}

The 'Entrada' class

class Entrada implements android.support.v4.app.LoaderManager.LoaderCallbacks<Bitmap> {
private String imgUrl, nombre,categoria;
private Bitmap image;
private entradaAdapter adapter;
Context context;
public Entrada(Context context, String nombre, String categoria, String imgFile) {
    this.context = context;
    this.nombre = nombre;
    this.categoria = categoria;
    this.imgUrl = "http://erasmusapp.byethost13.com/images/entries/"+imgFile;
}
@Override
public Loader<Bitmap> onCreateLoader(int id, Bundle args) {
    return new ImageLoadTask(context,imgUrl);
}
@Override
public void onLoadFinished(Loader<Bitmap> loader, Bitmap bitmap) {
    if (bitmap != null) {
        image = bitmap;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}

@Override
public void onLoaderReset(Loader<Bitmap> loader) {
}
public String getImgUrl() {
    return imgUrl;
}
public String getNombre() {
    return nombre;
}

public String getCategoria(){return categoria;}
public void setImgUrl(String imgUrl) {
    this.imgUrl = imgUrl;
}

public Bitmap getImage() {
    return image;
}

public entradaAdapter getAdapter() {
    return adapter;
}

public void setAdapter(entradaAdapter adapter) {
    this.adapter = adapter;
}

public void loadImagen(entradaAdapter adapter){
    this.adapter = adapter;
    if (imgUrl != null && !imgUrl.equals("")) {
        getLoaderManager().initLoader(0, null, this);
    }
}
}

And the second task:

class ImageLoadTask extends AsyncTaskLoader<Bitmap> {
String imgUrl;
Bitmap mBitmap;
public ImageLoadTask(Context context, String imgUrl){
    super(context);
    this.imgUrl = imgUrl;
}
@Override
protected void onStartLoading() {
    if (mBitmap != null) {
        deliverResult(mBitmap);
    }else {
        forceLoad();
    }
}

@Override
protected void onStopLoading() {
    cancelLoad();
}
public Bitmap loadInBackground() {

    Bitmap b = null;
    try {
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            b = BitmapFactory.decodeStream(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
@Override
public void deliverResult(Bitmap bitmap) {
    mBitmap = bitmap; // Caching
    super.deliverResult(mBitmap);
}
@Override
protected void onReset() {
    super.onReset();
    onStopLoading();
    mBitmap = null;
}

}

  • where is the call to `getLoaderManager().initLoader(LOADER_ID, null, callback);`? – njzk2 Jan 20 '15 at 16:34
  • 1. `new ImageLoadTask(context,imgUrl);` will not starts loading ... 2. `AsyncTaskLoaders` are for `Loaders` ... for jobs like this `AsyncTask` should be used 3. still loading all images will cause OutOfMemory sooner or later ... use some (already written) image loaders instead ... – Selvin Jan 20 '15 at 16:34
  • @njzk2 take a look at `loadImagen` method ... problem is that he is tring to use AsyncTaskLoader for wrong task – Selvin Jan 20 '15 at 16:35
  • Oh, this was a AsyncTask without loader before and I forgot to change that line, sorry. So my problem is other now, i will update the post – Adri Garrido Jan 20 '15 at 16:36
  • I cant use an AsyncTask because there are some tabs in the activity, and all of them have lists, so calling an AsyncTask can generate errors if you change from tabs and the fragment is destroyed before it ends. – Adri Garrido Jan 20 '15 at 16:38
  • 1
    ... `Entrada` should be a POJO class ... you are mixing principles ... if it is a simple data container it should not take care of loading ... – Selvin Jan 20 '15 at 16:38
  • `getLoaderManager` cannot compile, there is no such method in `Entrada`. This should be in a fragment or an activity. – njzk2 Jan 20 '15 at 16:45
  • The problem now is that i canr call getLoaderManager from `Entrada`. – Adri Garrido Jan 20 '15 at 16:46
  • If i put all in the fragment, I get an error who says that I can not call an Asynctaskloader from a non-static inner class, or some like that – Adri Garrido Jan 20 '15 at 16:53

0 Answers0