在ListView中将多个图像加载到ImageView中会造成混乱和不适应

时间:2013-10-12 17:35:05

标签: android listview android-listview android-asynctask android-arrayadapter

我从网上获取一些数据,然后使用我的ArrayAdapter将其传递给listView。

每一行都有自己的ID。和它自己的形象......也来自网络。

每次返回行之前,我都会启动AsyncTask ImageDownloader

但是当我在我现在面对的图像加载之前滚动列表视图时,它会造成混乱,并且随机图像会加载到错误行的ImageView中。

这就是我在我的适配器中所做的事情:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
    private final Context context;
    private final List<item> markers;
    private final static String[] a={"s"};

    ImageView image;
    ProgressBar loader;

    public MobileArrayAdapter(Context context, List<item> markers) {
        super(context, R.layout.list_item);
        this.context = context;
        this.markers = markers;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView title = (TextView) rowView.findViewById(R.id.title);
        loader = (ProgressBar) rowView.findViewById(R.id.loader);
        image = (ImageView) rowView.findViewById(R.id.itemimgae);
        TextView views = (TextView) rowView.findViewById(R.id.views);
        TextView likes=(TextView) rowView.findViewById(R.id.likes);
        TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
        TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
        TextView desc=(TextView) rowView.findViewById(R.id.desc);
        TextView pub =(TextView) rowView.findViewById(R.id.pub);
        TextView idnum =(TextView) rowView.findViewById(R.id.idnum);

        title.setText("      "+markers.get(position).getTitle());
        views.setText(markers.get(position).getView()+"");
        likes.setText(markers.get(position).getLike()+"");
        upvote.setText(markers.get(position).getUpvote()+"");
        downvote.setText(markers.get(position).getDownvote()+"");
        desc.setText(markers.get(position).getDesc());
        pub.setText(markers.get(position).getPub()+"");
        idnum.setText(markers.get(position).getId()+"");

        new ImageDownloader()
        .execute("http://www.myserver.page.php?h=400&img=upload/id"
                + markers.get(position).getId() + ".jpg");

        return rowView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (this.markers != null) ? this.markers.size() : 0;

    }

    private class ImageDownloader extends AsyncTask<String, String, Bitmap> {

        protected void onPreExecute(String res) {
            Log.i("Async-Example", "onPreExecute Called");

        }

        protected Bitmap doInBackground(String... param) {
            // TODO Auto-generated method stub
            return downloadBitmap(param[0]);
        }

        protected void onPostExecute(Bitmap result) {
            Log.i("Async-Example", "onPostExecute Called");
            image.setImageBitmap(result);
            loader.setVisibility(View.INVISIBLE);
        }

        private Bitmap downloadBitmap(String url) {
            // initilize the default HTTP client object
            final DefaultHttpClient client = new DefaultHttpClient();

            // forming a HttoGet request
            final HttpGet getRequest = new HttpGet(url);
            try {

                HttpResponse response = client.execute(getRequest);

                // check 200 OK for success
                final int statusCode = response.getStatusLine().getStatusCode();

                if (statusCode != HttpStatus.SC_OK) {
                    Log.w("ImageDownloader", "Error " + statusCode
                            + " while retrieving bitmap from " + url);
                    return null;

                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        // getting contents from the stream
                        inputStream = entity.getContent();

                        // decoding stream data back into image Bitmap that
                        // android understands
                        final Bitmap bitmap = BitmapFactory
                                .decodeStream(inputStream);

                        return bitmap;
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            } catch (Exception e) {
                // You Could provide a more explicit error message for
                // IOException
                getRequest.abort();
                Log.e("ImageDownloader", "Something went wrong while"
                        + " retrieving bitmap from " + url + e.toString());
            }

            return null;
        }

    }

}

X

我怎样才能解决这个烂摊子并让我的照片在滚动上方/下方之后保持原状?

1 个答案:

答案 0 :(得分:0)

如何在XML中定义ImageView?如果位图的大小不同,则图像视图会调整为特定大小,从而导致布局出现一些实际困难。

尝试在布局XML,图像视图定义中使用android:scaleType="centerInside"。 这将使列表看起来更加一致。

将加载的位图放到右视图中可能会出现secodn问题,尤其是在用户滚动并重复使用视图时。但我们先解决第一个问题: - )