-2

When i scroll my list View,images change places. I read about the problem, and kind of understand the reason. But i didn't found any solution to my problem

sorry for my english and my java, I am new to it :(

here is my getView :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        convertView = inflater.inflate(R.layout.list_item_layout, null);
        /****** View Holder Object to contain tabitem.xml file elements ******/


        holder = new ViewHolder();
        holder.name = (TextView) convertView
                .findViewById(R.id.bussiness_item_name);
        holder.phone = (TextView) convertView
                .findViewById(R.id.bussiness_item_telphone);
        holder.address = (TextView) convertView
                .findViewById(R.id.bussiness_item_address)
        holder.distance = (TextView) convertView
                .findViewById(R.id.bussiness_item_distance);
        holder.call = (Button) convertView
                .findViewById(R.id.bussiness_item_call_button);
        holder.call.setFocusable(false);
        holder.nav = (Button) convertView
                .findViewById(R.id.bussiness_item_nav_button);
        holder.nav.setFocusable(false);
        holder.logo = (ImageView)convertView.findViewById(R.id.logo_image);
        holder.logo.setTag(""+position);
        holder.logo.setVisibility(View.GONE);

        /************ Set holder with LayoutInflater ************/
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Business currentBussiness = getItem(position);

    if (currentBussiness.getName().contains("null")
            || currentBussiness.getName().equals("")) {
        holder.name.setText("---");
    } else {
        holder.name.setText(currentBussiness.getName());
    }

    /************ Set Model values in Holder elements ***********/
    if (!currentBussiness.getAddress().contains("null")) {
        holder.address.setText(currentBussiness.getAddress());
    } else {
        holder.address.setText("---");
    }
    if (!currentBussiness.getMain_phone().contains("null")) {
        holder.phone.setText(currentBussiness.getMain_phone());
    } else {
        holder.phone.setText("---");
    }

    if (!currentBussiness.getDistanceTo().contains("null") && !currentBussiness.getDistanceTo().equals("-1")) {
        if(Integer.valueOf( currentBussiness.getDistanceTo() ) < 999)
            holder.distance.setText(currentBussiness.getDistanceTo() + "מטר");
        else
            holder.distance.setText(Integer.valueOf(currentBussiness.getDistanceTo())/1000.0 + "ק\"מ");
    } else {
        holder.distance.setVisibility(View.GONE);
    }

    if (currentBussiness.getLogo().contains("null") || currentBussiness.getLogo().equals("")) {
        ImageLoader.getInstance().displayImage(null, holder.logo);
        holder.logo.setVisibility(View.GONE);
    } else {
        //holder.logo.setImageResource(R.drawable.default1);
        name = currentBussiness.getLogo();
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/cache");

        File file = new File (myDir, name);
        if (file.exists ()){

            Log.i("exist", "exsist");
            File f = new File(myDir+"/"+currentBussiness.getLogo());  
            Log.i("","f : "+f);
            String imageUri = ""+Uri.fromFile(f); 
            Log.i("","uri : "+imageUri);

            ImageLoader.getInstance().displayImage(imageUri, holder.logo);
            holder.logo.setVisibility(View.VISIBLE);
        }
        else{
            ImageLoader.getInstance().displayImage(dir+currentBussiness.getLogo(), holder.logo, new ImageLoadingListener() {


                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingFailed(String imageUri, View view,
                        FailReason failReason) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    view.setVisibility(View.VISIBLE);
                    saveToSD(loadedImage, name);
                    Log.i("downloaded","downloaded");

                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    // TODO Auto-generated method stub

                }
            });
        }
    }



    holder.call.setTag(currentBussiness.getMain_phone());
    holder.nav.setTag(currentBussiness.getAddress());

    holder.call.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String posted_by = (String) v.getTag();
            String uri = "tel:" + posted_by.trim();
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse(uri));
            mContext.startActivity(intent);
        }
    });

    holder.nav.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String address = (String) v.getTag();
            Uri location = Uri.parse("geo:0,0?q=" + address);
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
            mapIntent.setData(location);
            mContext.startActivity(mapIntent);
        }
    });


    return convertView;

}
Alex Ferents
  • 298
  • 4
  • 10
  • http://stackoverflow.com/questions/16789676/caching-images-and-displaying. check this example of UIL. the images don't change places. There is also a example in the docs to cache images and display in gridview without using third party libraries – Raghunandan May 15 '14 at 16:45

1 Answers1

-1

yes this happens because every time you scroll it again try to load the images.Univeralimageloader is a heavy image loader with much flexibility . But if you want your image to be loaded faster i found a fastest library to load image. UrlImageViewHelper it is easier to use and its fastest to loader. Try this at least once.

Abhishek
  • 119
  • 7