0

我有一个 Listview 的 fagment,里面有一个 ImageView 和一个 Textview。所以当片段创建时,我调用一个 AsyncTaskLoader 来填充它。在 onLoadFinished 方法中,我为列表设置了适配器。问题是我无法访问 getLoadedManager :S

这是代码:这是第一个任务的 onLoadFinishedMethod

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);
    }
}

适配器:

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;
}
}

'Entrada' 类

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);
    }
}
}

第二个任务:

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;
}

}

4

0 回答 0