2015-10-28 69 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

先生,請您在我的代碼中提供一些建議,這對我來說非常重要 –

相關問題