ListView跳过第2行和第3行中的位图图像。如何填充所有行?

时间:2016-08-18 07:18:45

标签: java android listview

问题是getView()方法正在跳过以查看其他图像,简而言之,getView()仅显示ListView的第一行和最后一行。

如何填充ListView中的所有行?

这是我的CustomListAdapter中的getView()代码,没有包含URL 我只是直接从API下载图像。

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

    RowItemLoyalty rowItem = getItem(position);
    if (inflater == null) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new Holder();


        holder.title = (TextView) convertView.findViewById(R.id.description);
        holder.exp = (TextView) convertView.findViewById(R.id.promotion_expiration);
        holder.tokensFor = (TextView) convertView.findViewById(R.id.tokensForRedeemOffer);
        holder.promotionImages = (ImageView) convertView.findViewById(R.id.promotion_image);

        convertView.setTag(holder);

    } else {
        holder = (Holder) convertView.getTag();
    }

    holder.title.setText(rowItem.getDescription());
    holder.exp.setText(rowItem.getDateEnd());
    holder.tokensFor.setText(rowItem.getTokensFor());
    //holder.promotionImages.setImageBitmap(rowItem.getImage1());

    try {
        image_id.put(ConstantKeys.ID, rowItem.getImageId());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    ImageDownloadRequest2 request = new ImageDownloadRequest2();
    SharedPreferences sharedPreferences = context.getSharedPreferences("USERPASS", 0);
    String email = sharedPreferences.getString("username", null);
    String password = sharedPreferences.getString("password", null);
    request.getToken(email, password, new ApiRequestListener() {
        @Override
        public void onSuccess(Object object) {
            (new GetImageTask() {
                @Override
                protected void onPostExecute(Bitmap data) {
                    super.onPostExecute(data);
                    if (data != null) {
                        Bitmap[] holderImage = new Bitmap[] {data};

                        holder.promotionImages.setImageBitmap(holderImage[0]);
                        Log.d("BITMAPVALUE", String.valueOf(data));
                    }else
                    {
                        holder.promotionImages.setImageResource(R.drawable.app_icon);
                    }
                }
            }).execute();
        }

        @Override
        public void onError(String error) {
            Log.e("Get Logo", error);
        }
    });


    Log.d("IMAGESTASHLOYALTY", String.valueOf(rowItem.getImageId()));

    return convertView;
}

这是我的AsyncTask

private static class GetImageTask extends AsyncTask<Void, Void, Bitmap>
{
        @Override
        protected Bitmap doInBackground(Void... params)
        {
            ImageDownload2 manager = ImageDownload2.getInstance();
            Bitmap result = null;

            try
            {
      result = manager.apiCall("File/DownloadFiles", image_id.toString(), "C");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            return result;
        }
}

1 个答案:

答案 0 :(得分:0)

回收意见的标准误解。您将AsyncTask作为holder作为参数。但是,当asynctask准备下载图像时,持有者将成为不同的持有者。

您最好将int position作为参数提供给AsyncTask,然后您可以将图像放在最高位置。