0

I have a ListView inside an Activity and each of its item is customized to have some TextViews along with a DropDownList item and an ImageView. Inside an OnScrollListener() implementation each item of a listView gets populated using an ArrayAdapter populating text views with values taken from an arrayList and ImageView with the .jpeg file stored on SD card. Following is the screenSHot of listView Item List View Item

The problem arises when the .jpeg file from sdCard is converted to a bitmap (i.e. BitmapFactory.decodeFile(fileName) ) and then gets assigned to an image View using setImageBitmap(Bitmap bmp). As the setting bitmap image to an image view is a lengthy process it cannot keep pace with the scroll listener implementation and the ImageView of different ListView rows gets populated with the image it was assigned to any row above. Can anybody please suggest some workout to cater this issue specifically the assignment of images from SD Card to an imageView. Its not like my listView item is overLoaded with controls that is why i am facing this problem. I also have tried it with single ImageView item inside each row and it behaves the same way. Your suggestion to improvise this are welcome and surely will be of great help. Thank you :-)

@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);
    }
}
Abdul Rehman
  • 373
  • 6
  • 15
  • you'll need a asynctask for the loading. – njzk2 Sep 28 '12 at 07:57
  • I struggled with this issue for a long time. Finally I managed to do it by making the method(which returned the bitmap) "synchronized". – Yogesh Somani Sep 28 '12 at 07:57
  • Hi njzk2 I have cross checked my implementation without onScrollListener too but it produces the same resulrt. I have edited my question to add Adapter implementation code in it. Will you please review it to suggest a problem with it – Abdul Rehman Sep 28 '12 at 10:39

1 Answers1

1

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

This will be hopefull to you :P

Arnold
  • 273
  • 1
  • 11
  • The content you suggested was quite informative but didn't helped me with the issue. Even after implementing an Asynchronous class for image to bitmap conversion the result onScrollListener causing same issue as it was doing before. Image gets mapped on ImageView of random rows. – Abdul Rehman Sep 28 '12 at 10:37