自定义listView与通用图像加载器,随机播放我的图像

时间:2014-05-15 16:27:18

标签: java android android-listview universal-image-loader

当我滚动列表视图时,图像会更改位置。 我读到了这个问题,并且理解了原因。 但我没有找到解决问题的方法

抱歉我的英语和我的java,我是新手:(

这是我的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;

}

1 个答案:

答案 0 :(得分:-1)

是的,这是因为每次再次滚动它都会尝试加载图像.Univeralimageloader是一个具有很大灵活性的重型图像加载器。但是如果你想让你的图像加载更快,我找到了一个最快的库来加载图像。 UrlImageViewHelper它更容易使用,并且加载速度最快。至少尝试一次。