2016-12-07 46 views
-1

我有一個列表視圖與列表項中的圖像。我正在使用listview的自定義適配器。我使用AsyncTask爲適配器中的每個列表項目下載圖像。我的自定義接口的代碼:在列表視圖中正確加載圖像

public class AdapterUcomment extends ArrayAdapter<UserModel> { 


public AdapterUcomment(Activity context, List<UserModel> list) { 
    super(context, R.layout.layout_comment_item, list); 
    this.context = context; 
    this.list = list; 
} 

class ViewHolder { 
    protected TextView text1, text2, text3; 
    protected ImageView imagev; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = null; 
    if (convertView == null) { 

     LayoutInflater inflator = context.getLayoutInflater(); 
     view = inflator.inflate(R.layout.layout_comment_item, null); 
     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.text1 = (TextView) view.findViewById(R.id.txtv_uname); 
     viewHolder.text2 = (TextView) view.findViewById(R.id.txtv_comment); 
     viewHolder.text3 = (TextView) view.findViewById(R.id.txtv_cmntTime); 
     viewHolder.imagev = (ImageView) view.findViewById(R.id.imgv_upic); 
     view.setTag(viewHolder); 

    } else { 
     view = convertView; 
    } 
    positionPic = position; 
    final ViewHolder holder = (ViewHolder) view.getTag(); 
    String uname = list.get(position).getUname(); 
    String ufbid = list.get(position).getUfbid(); 
    String ucomment = list.get(position).getUcomment(); 
    String ucomment_time = list.get(position).getUcomment_time(); 


    holder.text1.setText(Html.fromHtml(uname)); 
    holder.text2.setText(Html.fromHtml(ucomment)); 
    holder.text3.setText(Html.fromHtml(ucomment_time)); 

    // downloading the image using AsyncTask 

    new UserPic(position, ufbid, holder.imagev).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

    return view; 
} 


public class UserPic extends AsyncTask<String, Void, String> { 
    String ufbid = null; 
    Bitmap bitmap = null; 
    ImageView imageView; 
    int pos = 0; 
    public UserPic(int pos, String ufbid, ImageView imageView) { 
     this.ufbid = ufbid; 
     this.imageView = imageView; 
     this.pos = pos; 
    } 

    @Override 
    protected String doInBackground(String... strings) { 
     URL imageURL = null; 
     try { 
      imageURL = new URL("......."); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     imageView.setImageBitmap(bitmap); 
     super.onPostExecute(s); 

    } 
} 

一切似乎乍一看像這樣工作正常: image of listview with properly loaded images 但問題開始時我向下滾動到最後一個項目,然後再向上滾動到第一個項目那麼第一個列表項圖像消失像這樣:image of listview with improperly loaded image 那麼我如何擺脫這一點,並正確加載在列表視圖中正確的圖像? 在此先感謝。

+0

請使用圖像加載程序庫。鏈接:https://github.com/nostra13/Android-Universal-Image-Loader –

+0

問題被問到bazillion次......它因爲視圖重用... 1. getView調用位置1你開始asynctask1 2. getView是再次調用位置2,來自點1的視圖被重用,並且您正在開始asynctask2 ... 3. asynctask2完成並且右圖像被設置4. asynctask1完成並設置錯誤的圖像... **簡單的解決方案(不是對於生產應用程序)將URL存儲爲imageview中的標記在設置位圖之前將其與傳遞給AsyncTask的URL進行比較...如果它不同,請不要設置** – Selvin

回答

1

使用類似於畢加索或Glide的圖像加載庫來加載圖像到列表視圖。另外,你有沒有考慮過使用recyclerview?

這裏是畢加索的更多信息: http://square.github.io/picasso/

+0

我正在使用Picasso庫,但仍然收到相同的錯誤。任何想法? –

+0

終於擺脫了這個問題,通過使用Recyclerview和Glide庫 –