自定义列表视图不显示AsyncTask

时间:2014-02-28 09:22:16

标签: android listview android-asynctask

我正在使用代码将照片列表从图库加载到自定义水平列表视图中。 Listview滚动不顺利。我决定把加载照片放到背景中。但现在listview没有显示任何内容。你能帮我一下吗?经过一番讨论后,我做了一些修正。但结果显示空列表视图。

Main.java

    public class Main extends Activity {
private HorizontalImageAdapter horizontalimageAdapter;
 private HorizontalListView listview;
 private Utilf utils;
 private ArrayList<String> imagePaths = new ArrayList<String>();

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_horizontal_list_view);

         utils = new Utilf(this);
         imagePaths = utils.getFilePaths();
         listview = (HorizontalListView) findViewById(R.id.gallery);
         horizontalimageAdapter = new HorizontalImageAdapter(this, imagePaths);
         listview.setAdapter(horizontalimageAdapter);
     }
    }

Horizo​​ntalImageAdapter.java

public class HorizontalImageAdapter extends BaseAdapter {

 private Activity context;
 private static ImageView imageView;
 private ArrayList<String> plotsImages;
 private LayoutInflater l_Inflater;

 public HorizontalImageAdapter(Activity context, ArrayList<String> plotsImages) {
  this.context = context;
  this.plotsImages = plotsImages;
  l_Inflater = LayoutInflater.from(context);
 }

 @Override
 public int getCount() {
     return plotsImages.size();
 }

 @Override
 public Object getItem(int position) {
  return null;
 }

 @Override
 public long getItemId(int position) {
  return 0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
     ViewHolder holder;
  if (convertView == null) {
   convertView = l_Inflater.inflate(R.layout.listview_item, null);
   holder = new ViewHolder();
   holder.imageView = (ImageView) convertView.findViewById(R.id.icon);

   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  if (holder.imageView != null) {
      new ImageDownloaderTask(holder.imageView).execute(plotsImages.get(position));
  }

  return convertView;

 }

 private static class ViewHolder {
  ImageView imageView;
 }
}

ImageDownloader.java

 class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
            private final WeakReference<ImageView> imageViewReference;
            private int imageWidth = 96;

            public ImageDownloaderTask(ImageView imageView) {
                imageViewReference = new WeakReference<ImageView>(imageView);
            }

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = loadBitmap(params[0]);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {

                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    } else {
//                      imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.list_placeholder));
                    }
                }

            }
        }

2 个答案:

答案 0 :(得分:0)

  • 列出项目

    请检查您的getItem(int position)方法。它返回null。相反,您必须从该位置本身的列表中返回Object

  • getView()方法

您正在直接返回convertView。而是将其放在一个单独的变量中,如此代码

 public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
      LayoutInflater inflater = context.getLayoutInflater();
      rowView = inflater.inflate(R.layout.rowlayout, null);
      // configure view holder
      ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
      viewHolder.image = (ImageView) rowView
          .findViewById(R.id.ImageView01);
      rowView.setTag(viewHolder);
    }

    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names[position];
    holder.text.setText(s);
    if (s.startsWith("Windows7") || s.startsWith("iPhone")
        || s.startsWith("Solaris")) {
      holder.image.setImageResource(R.drawable.no);
    } else {
      holder.image.setImageResource(R.drawable.ok);
    }

    return rowView;
  }

代码来自this链接

答案 1 :(得分:0)

您不能这样做,因为您的imageview将被多次重复使用。 你需要一种方法来检查一旦asynctask完成加载位图你仍然在listview的完全相同的行上,否则你将在滚动列表视图时显示随机位置的图像。为此,您可以将位图URL作为标记存储在imageview上,并在调用setImageBitmap()之前检查该标记。如果匹配,则可以显示位图,否则不执行任何操作。

但是像@HERO那样说你为什么不使用Picasso lib来以更好的方式做到这一点;)