图像未在ListView中以正确的顺序显示

时间:2013-12-13 10:25:44

标签: android android-listview

我在后台使用ListViewAsyncTask中显示图片。所有正在下载的图像但未以正确的顺序/顺序显示。

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;
    ViewHolder holder = null;
    public CustomListViewAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
       // TextView txtTitle;
        TextView name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            /*holder.txtTitle = (TextView) convertView.findViewById(R.id.title);*/
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(rowItem.getName());
     //   holder.txtTitle.setText(rowItem.getTitle());


        //holder.imageView.setImageResource(rowItem.getImageId());

        GetXMLTask task = new GetXMLTask();
        System.out.println("rowItem.getImageUrl()="+rowItem.getImageUrl());
        if(rowItem.getImageUrl()!=null)
         task.execute(rowItem.getImageUrl());
        else
            holder.imageView.setImageResource(R.drawable.mediumphoto);
      //  Bitmap bitMap=downloadImage(rowItem.getImageUrl());
        //holder.imageView.setImageBitmap(bitMap);
//      holder.imageView.setVisibility(ImageView.VISIBLE);
        return convertView;
    }





    private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap map = null;
            for (String url : urls) {
                map = downloadImage(url);
            }
            return map;
        }

        // Sets the Bitmap returned by doInBackground
        @Override
        protected void onPostExecute(Bitmap result) {
            holder.imageView.setImageBitmap(result);
        }

        // Creates Bitmap from InputStream and returns it
        private Bitmap downloadImage(String url) {
            Bitmap bitmap = null;
            InputStream stream = null;
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            try {
                stream = getHttpConnection(url);
                bitmap = BitmapFactory.
                        decodeStream(stream, null, bmOptions);
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return bitmap;
        }

        // Makes HttpURLConnection and returns InputStream
        private InputStream getHttpConnection(String urlString)
                throws IOException {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return stream;
        }
    }
} 

新代码

package com.example.gdirectory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
       // TextView txtTitle;
        TextView name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        RowItem rowItem = getItem(position);
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            /*holder.txtTitle = (TextView) convertView.findViewById(R.id.title);*/
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(rowItem.getName());
     //   holder.txtTitle.setText(rowItem.getTitle());


        //holder.imageView.setImageResource(rowItem.getImageId());

        GetXMLTask task = new GetXMLTask(holder.imageView);
        System.out.println("rowItem.getImageUrl()="+rowItem.getImageUrl());
        if(rowItem.getImageUrl()!=null)
         task.execute(rowItem.getImageUrl());
        else
            holder.imageView.setImageResource(R.drawable.mediumphoto);
      //  Bitmap bitMap=downloadImage(rowItem.getImageUrl());
        //holder.imageView.setImageBitmap(bitMap);
//      holder.imageView.setVisibility(ImageView.VISIBLE);
        return convertView;
    }





    private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {

        private ImageView imageView;
        private int position;
        public GetXMLTask(ImageView view) {
            imageView = view;
            position = (Integer) imageView.getTag();
        }

        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap map = null;
            for (String url : urls) {
                map = downloadImage(url);
            }
            return map;
        }

        // Sets the Bitmap returned by doInBackground
        @Override
        protected void onPostExecute(Bitmap result) {
            //imageView.setImageBitmap(result);
            //holder.imageView.setImageBitmap(result);
             int pos = (Integer) imageView.getTag();
             if(position == pos) {
                 imageView.setImageBitmap(result);
             }
        }

        // Creates Bitmap from InputStream and returns it
        private Bitmap downloadImage(String url) {
            Bitmap bitmap = null;
            InputStream stream = null;
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            try {
                stream = getHttpConnection(url);
                bitmap = BitmapFactory.
                        decodeStream(stream, null, bmOptions);
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return bitmap;
        }

        // Makes HttpURLConnection and returns InputStream
        private InputStream getHttpConnection(String urlString)
                throws IOException {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return stream;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

步骤1)按如下方式更新getView()方法:

public View getView(int position, View convertView, ViewGroup parent) {

    RowItem rowItem = getItem(position);

    if (convertView == null) {
        holder = new ViewHolder();

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(rowItem.getName());
    holder.imageView.setTag(position); // this line
    GetXMLTask task = new GetXMLTask(holder.imageView); // this line
    System.out.println("rowItem.getImageUrl()="+rowItem.getImageUrl());
    if(rowItem.getImageUrl()!=null)
       task.execute(rowItem.getImageUrl());
    else
       holder.imageView.setImageResource(R.drawable.mediumphoto);
    return convertView;
}

我们将ImageView传递给AsyncTask并设置ImageView的标记作为位置。

第2步:更新您的AsyncTask,如下所示:

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;
    private int position;
    public GetXMLTask(ImageView view) {
        imageView = view;
        position = (int)imageView.getTag();
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        int pos = (int)imageView.getTag();
        if(position == pos) {
            imageView.setImageBitmap(result);
        }
    }
    ...
    ...
    // rest same

因此,当AsyncTask完成后,我们会检查ImageView标记。如果标签与我们之前的图像相同。