android的懒惰图像加载ListView无法正常工作

时间:2016-04-26 11:08:13

标签: android image listview

我想把懒人加载图片放到我的ListView中,图片加载正常,但我有一个问题。加载图像时可以互换。

假设ListView有10行。它加载第一行的图像,它在第一行显示,然后加载第二行的图像。它会在第2行显示片刻,然后显示第1行第2行的图像。然后row1中的ImageView在第1行和第2行的图像之间切换。同样在加载下一行的图像时。前一行的图像之间切换。然后在加载所有图像后,一切都会正确显示。

这是我的代码

Adapater类:

public class FamilyMemberListAdapter extends ArrayAdapter<Map<String, String>> {
    List<Map<String, String>> familyMemberList = new ArrayList<Map<String, String>>();
    private Activity activity;

    public FamilyMemberListAdapter(Activity activity,
            List<Map<String, String>> familyMemberList) {
        super(activity, R.layout.activity_gch_family_members, familyMemberList);
        this.activity = activity;
        this.familyMemberList = familyMemberList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext()).inflate(
                    R.layout.activity_gch_family_member_item, parent, false);
            holder = new ViewHolder();
            holder.lblFamilyMemberName = (TextView) convertView
                    .findViewById(R.id.lblFamilyMemberItem);
            holder.lblFamilyMemberRelation = (TextView) convertView
                    .findViewById(R.id.lblFamilyMemberRelationItem);
            holder.imgProfilePic = (ImageView) convertView
                    .findViewById(R.id.imgvProfilePic);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        int accountId = Integer.valueOf(familyMemberList.get(position).get(
                "accountId"));
        holder.lblFamilyMemberName.setText("Name: "
                + familyMemberList.get(position).get("name"));
        holder.lblFamilyMemberRelation.setText("Relation: "
                + familyMemberList.get(position).get("relation"));
        if (holder.imgProfilePic != null) {
            new ImageDownloaderTask(holder.imgProfilePic).execute(String
                    .valueOf(accountId));
        }
        return convertView;
    }

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

    static class ViewHolder {
        TextView lblFamilyMemberName;
        TextView lblFamilyMemberRelation;
        ImageView imgProfilePic;
    }
}

Imageloader AsyncTask:

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

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

    @Override
    protected Bitmap doInBackground(String... params) {
        String responseText = null;
        HttpClient httpClient = ServiceHelper.getHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(RestApiPaths.GET_PROFILE_PIC + accountId);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            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();
                }
            }
            Log.d(TAG, responseText);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @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 {
                    Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.holder_pic_side);
                    imageView.setImageDrawable(placeholder);
                }
            }
        }
    }
}

我为ListView项创建了一个Static类。仍然为什么图像在加载时继续交换。请告诉我这里我做错了什么。

2 个答案:

答案 0 :(得分:1)

在调用AsyncTask之前添加onPostExecute。在您的任务中添加额外的参数accountId。然后在<script type="text/javascript"> $(document).ready(function () { //Test values var intensity = 5; var trackerList = ["a", "b", "c", "d", "e"]; var registeredTrackableObjects = ["a", "b", "c", "d"]; var xDistance = 1.05;//Distance between bars var yMin = 0; var yMax = 20; var xMin = 0; var xMax = 10; var data = [];//Flot JS format, which contains pairs of {label, array} var customBarArray = []; var customBar = []; //Create a data set for every TO for(i = 0; i<registeredTrackableObjects.length; i++) { //Create 1 bar for every tracker customBar = []; for(j = 0; j<trackerList.length; j++) { customBar[j] = j; customBar.push([xDistance * j, intensity]); } customBarArray.push(customBar); var name = "Beacon: " + i; data.push({label: name, data: customBarArray[i]}); xDistance += 1.05; } $.plot($("#placeholder"), data, { series: { bars: { show: true, barWidth: 0.1, lineWidth: 0, order: 1, fillColor: { colors: [{ opacity: 1 }, { opacity: 0.7 }] } } }, xaxis: { mode: "time", min: xMin, max: xMax, tickLength: 0, tickSize: [1, "month"], axisLabel: 'Month', axisLabelUseCanvas: true, axisLabelFontSizePixels: 10, axisLabelFontFamily: 'Verdana, Arial, Helvetica, Tahoma, sans-serif', axisLabelPadding: 1 }, yaxis: { min: yMin, max: yMax, axisLabel: 'Value', axisLabelUseCanvas: true, axisLabelFontSizePixels: 10, axisLabelFontFamily: 'Verdana, Arial, Helvetica, Tahoma, sans-serif', axisLabelPadding: 5 }, legend: { backgroundColor: "#EEE", labelBoxBorderColor: "none" }, colors: ["#AA4643", "#89A54E", "#4572A7"] }); }); </script>中检查它是否与图像视图中的帐户ID相同

答案 1 :(得分:0)

也许尝试使用像毕加索或通用图像加载器之类的库。 他们用图像加载解决了大部分问题