使用图像加载器android时不显示图像

时间:2014-10-07 12:56:51

标签: android lazylist

我正在使用图像加载器来显示json解析中的图像和文本。文本正在显示但图像未显示。没有错误,但它显示默认图标。有人请帮助我为什么图像不显示。我的代码如下:

 public class GridAdapter extends ArrayAdapter<Persons> {
 ArrayList<Persons> personList;
 LayoutInflater layoutInflater;
 int Resource;
 ViewHolder viewHolder;
 ImageLoader imageLoader; // declared image laoder

 public GridAdapter(Context context, int resource, ArrayList<Persons> objects) {
    super(context, resource, objects);
    layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    personList = objects;
    }
   @Override
  public int getCount() {
  // TODO Auto-generated method stub
return super.getCount();
  }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
 imageLoader = new ImageLoader(getContext()); // since thrown null pointer exception when i   declared this, it's not declaring null pointer exception but image is not displaying, default icon   set in ImageLoader is displaying..

if(v==null){
    viewHolder = new ViewHolder();
    v = layoutInflater.inflate(Resource, null);
    viewHolder.tvName=(TextView)v.findViewById(R.id.tvname);
    viewHolder.imageview=(ImageView)v.findViewById(R.id.picture);
    v.setTag(viewHolder);


}
else{
    viewHolder = (ViewHolder) v.getTag();

    }
   viewHolder.tvName.setText(personList.get(position).getName());
  imageLoader.DisplayImage(personList.get(position).getImage(),viewHolder.imageview);// this is not working..
// TODO Auto-generated method stub
  return v;
  }
static class ViewHolder {
public ImageView imageview;
public TextView tvName;


  }

}

注意:

如果我使用它,

私有类DownloadImageTask扩展了AsyncTask {         ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }

}

并在imageview中设置为

new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());

它正在工作..但每次当我向下滚动并向上移动时,图像都会被下载。我需要在缓存中显示。所以我使用了图像加载器。但它没有显示图像。

2 个答案:

答案 0 :(得分:0)

我建议您使用ION库来加载这样的图像。使用该库,只需要这段代码就可以异步地从URL下载图像:

        String url = datamap.get(image1);
        Ion.with(holder.userImage).placeholder(R.drawable.new_loading)
                .error(R.drawable.loading_animation).load(url);

答案 1 :(得分:0)

要从服务器异步加载图像,您可以使用Picasso小型库。这是gradle

compile 'com.squareup.picasso:picasso:2.3.3'

这是你的ImageView

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

这是简单的java代码

ImageView imageView = (ImageView) findViewById(R.id.imageView);

    Picasso.with(this)
            .load("YOUR_IMAGE_URL")
            .into(imageView);

那就是它。有关详细信息,请访问here