如何在ListView中处理ImageView?

时间:2012-09-28 07:52:56

标签: android android-layout android-listview android-imageview android-scroll

我在一个Activity中有一个ListView,它的每个项目都是自定义的,有一些 TextViews 以及 DropDownList 项和 ImageView 。在 OnScrollListener()实现中,listView的每个项目都使用ArrayAdapter填充文本视图,其中的值取自arrayList和ImageView,其中 .jpeg 文件存储在SD卡上。以下是listView Item的screenSHot List View Item

当来自sdCard的.jpeg文件转换为位图(即 BitmapFactory.decodeFile(fileName))然后使用 setImageBitmap(位图bmp)分配给图像时出现问题)即可。由于将位图图像设置为图像视图是一个漫长的过程,它无法跟上滚动侦听器实现的步伐,并且不同ListView行的ImageView将填充其分配给上面任何行的图像。任何人都可以建议一些锻炼来解决这个问题,特别是将图像从SD卡分配到imageView。它不像我的listView项目是overLoaded控件,这就是为什么我面临这个问题。我也尝试过每行中的单个ImageView项,它的行为方式相同。您对即兴表演的建议是值得欢迎的,肯定会有很大的帮助。谢谢: - )

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

    View row = convertView;
    AssetDetailHolder assetDetailholder = null;

    try {

        if (row == null) {

            LayoutInflater inflator = ((Activity) context)
                    .getLayoutInflater();
            row = inflator.inflate(layoutResourceID, parent, false);

            assetDetailholder = new AssetDetailHolder();

            assetDetailholder.itemPosition = position;

            assetDetailholder.txtVwlineCOde = (TextView) row
                    .findViewById(R.id.lineCodeValue_ad);
            assetDetailholder.txtvwLocation = (TextView) row
                    .findViewById(R.id.locationValue_ad);
            assetDetailholder.txtvwLocationDetail = (TextView) row
                    .findViewById(R.id.detailLocationValue_ad);
            assetDetailholder.txtvwInventoryNo = (TextView) row
                    .findViewById(R.id.InventoryNoValue_ad);
            assetDetailholder.spnrconditionCode = (Spinner) row
                    .findViewById(R.id.spinner_ad);
            assetDetailholder.txtvwAssetName = (TextView) row
                    .findViewById(R.id.AssetNameValue_ad);
            assetDetailholder.subNoThumbnail = (ImageView) row
                    .findViewById(R.id.IV_subNoThumbnail);

            row.setTag(assetDetailholder);
        } else {
            assetDetailholder = (AssetDetailHolder) row.getTag();
            assetDetailholder.itemPosition = position;
        }

        AssetDetail assetDetail = assetsDetailList[position];

        new ThumbnailTask(position, assetDetailholder, assetDetail, context)
                .execute();

        if (assetDetail.assetLineCodeDesc.equals("")) {
            assetDetailholder.txtVwlineCOde
                    .setText(assetDetail.strLineCOde);
        } else {
            assetDetailholder.txtVwlineCOde.setText(assetDetail.strLineCOde
                    + "(" + assetDetail.assetLineCodeDesc + ")");
        }

        if (assetDetail.assetLocationNameDesc.equals("")) {
            assetDetailholder.txtvwLocation
                    .setText(assetDetail.strLocationName);
        } else {
            assetDetailholder.txtvwLocation
                    .setText(assetDetail.strLocationName + "("
                            + assetDetail.assetLocationNameDesc + ")");
        }

        assetDetailholder.txtvwLocationDetail
                .setText(assetDetail.strLocationDetail);

        if (assetDetail.strInventoryNumber.contains("-")) {
            assetDetailholder.txtvwInventoryNo
                    .setText(assetDetail.strInventoryNumber.split("-")[0]);
        } else {
            assetDetailholder.txtvwInventoryNo
                    .setText(assetDetail.strInventoryNumber);
        }
        assetDetailholder.txtvwAssetName.setText(assetDetail.assetName);

        String conditionCodeString = assetDetail.assetConditionCode;

        if (conditionCodeString != "" || conditionCodeString != null) {
            try {
                int conditionCodeInteger = Integer
                        .parseInt(conditionCodeString);
                assetDetailholder.spnrconditionCode
                        .setSelection(conditionCodeInteger);
            } catch (Exception e) {
                assetDetailholder.spnrconditionCode.setSelection(0);
            }
        } else {
            assetDetailholder.spnrconditionCode.setSelection(0);
        }

        // String thumbnailDir = Common
        // .getSubNoDirectory(context, assetDetail);

        // if (new File(thumbnailDir).isDirectory()) {
        //
        // File thumbnailFile = new File(Common.getSubNoImgFilePath(
        // thumbnailDir, assetDetail, SubNo_ImageSample.A));
        //
        // if (thumbnailFile.exists()) {
        // assetDetailholder.subNoThumbnail
        // .setImageBitmap(BitmapFactory
        // .decodeFile(thumbnailFile.getAbsolutePath()));
        // }
        // }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return row;
}

static class AssetDetailHolder {
    TextView txtVwlineCOde;
    TextView txtvwLocation;
    TextView txtvwLocationDetail;
    TextView txtvwInventoryNo;
    TextView txtvwAssetName;
    Spinner spnrconditionCode;
    ImageView subNoThumbnail;
    public int itemPosition;
}

private static class ThumbnailTask extends AsyncTask<Void, Void, Void> {

    private int mPosition;
    private AssetDetailHolder mHolder;
    private Context cntxt;
    private AssetDetail assetItem;

    private Bitmap thumbnailBmp;

    public ThumbnailTask(int position, AssetDetailHolder holder,
            AssetDetail asset, Context context) {
        mPosition = position;
        mHolder = holder;
        assetItem = asset;
        cntxt = context;
    }

    @Override
    protected Void doInBackground(Void... params) {

        String thumbnailDir = Common.getSubNoDirectory(cntxt, assetItem);

        if (new File(thumbnailDir).isDirectory()) {

            File thumbnailFile = new File(Common.getSubNoImgFilePath(
                    thumbnailDir, assetItem, SubNo_ImageSample.A));

            if (thumbnailFile.exists()) {

                thumbnailBmp = BitmapFactory.decodeFile(thumbnailFile
                        .getAbsolutePath());
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (mHolder.itemPosition == mPosition && thumbnailBmp != null) {
            mHolder.subNoThumbnail.setImageBitmap(thumbnailBmp);
        }
        // super.onPostExecute(result);
    }
}

1 个答案:

答案 0 :(得分:1)