2015-10-28 70 views
0

你好,我正在使用列表视图,并显示从服务器的图像到它。来自服务器的图像不显示在列表视图

但问题是,所有加载的图像都显示在最后一个项目中,而不是显示在相应的位置。

请帮我显示在相应的位置图像

public class Offer_adapter extends ArrayAdapter<String> { 
Context context1; 
String[] offer_title; 
String[] offerimg1; 
String[] mrp; 
String[] offerprice; 
String[] you_save; 
String[] imgURLArray; 
Bitmap bitmap; 
ImageView offerimg; 
int a; 
LayoutInflater inflater1; 

public Offer_adapter(Context context1, String[] offer_title, String[] offerimg1, String[] mrp, String[] you_save, String[] offerprice) { 
    super(context1, R.id.offer_list, offer_title); 
    this.context1 = context1; 
    this.offer_title = offer_title; 
    this.offerimg1 = offerimg1; 
    this.mrp = mrp; 
    this.offerprice = offerprice; 
    this.you_save = you_save; 
} 

private static class ViewHolder { 

    String offerimg; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder viewHolder = null; 
    if (convertView == null) { 
     inflater1 = (LayoutInflater) context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater1.inflate(R.layout.offer_list, null); 
     viewHolder = new ViewHolder(); 

    } 
    android.util.Log.v("abhi", "" + position); 
    imgURLArray = new String[position + 1]; 
    for (int i = 0; i <= position; i++) { 
     android.util.Log.v("abhijit", "" + position); 
     imgURLArray[i] = "http://www.surun.co/preost/mod_offer/images/" + offerimg1[position]; 
     android.util.Log.v("abhi", "" + imgURLArray[position]); 
    } 
    a=position; 
    viewHolder = (ViewHolder) convertView.getTag(); 
    TextView offertitle = (TextView) convertView.findViewById(R.id.ofrtitle); 
    TextView offermrp = (TextView) convertView.findViewById(R.id.offeroriginal); 
    offermrp.setPaintFlags(offermrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
    TextView offersave = (TextView) convertView.findViewById(R.id.saveoffer); 
    TextView ofrprice = (TextView) convertView.findViewById(R.id.priceoffer); 
    offerimg = (ImageView) convertView.findViewById(R.id.ofr_img); 

    offertitle.setText(offer_title[position]); 
    offermrp.setText("Original Price: \u20B9" + mrp[position]); 
    offersave.setText("You Save: \u20B9" + you_save[position]); 
    ofrprice.setText("Offer Price: \u20B9" + offerprice[position]); 
    // Bitmap imageBitmap = null; 


    new DownloadAsyncTask().execute(imgURLArray[position]); 
    Log.v("abhi","async"); 
    return convertView; 
} 

private class DownloadAsyncTask extends AsyncTask<String, String, Bitmap> { 

    protected Bitmap doInBackground(String... args) { 
     try { 
      Log.v("abhi","in do background"); 
      bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent()); 
      bitmap = Bitmap.createScaledBitmap(bitmap, 270, 375, true); 

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

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     super.onPostExecute(bitmap); 
     if (bitmap !=null) { 
     offerimg.setImageBitmap(bitmap); 
     } else { 

      offerimg.setImageResource(R.drawable.nooffer); 
     } 

    } 
} 
    } 

,所有图像都显示在最后一个项目一个接一个。我只是想在相应的位置上展示它。

+0

使用[毕加索](http://square.github.io/picasso/)缓存和加载图像位图到你的imageview。你也没有正确使用视图模式。 – Emil

+0

尝试以下链接:http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134 – KishuDroid

+0

先生,你可以请给一些suggection在我的代码,请这对我来说 –

回答

2

你应该使用lazyloading代替下载图像作为位图。
位图将创建某个问题或会给你一些设备

有很多图像加载库可用于Android内存不足的错误。 看一看这些

https://github.com/square/picasso
https://github.com/nostra13/Android-Universal-Image-Loader
https://code.google.com/p/android-query/wiki/ImageLoading
https://android.googlesource.com/platform/frameworks/volley
https://github.com/koush/UrlImageViewHelper
https://github.com/novoda/image-loader

+0

非常重要的我是新到机器人,所以你可以请缓解我的问题。请纠正我的问题。 –

+0

先生,请您在我的代码中提供一些建议,这对我来说非常重要 –

相关问题