2015-08-31 93 views
1

我有一個GridView與圖像和TextViews。使用異步任務從URL加載圖像。GridView圖像加載不正確

這裏是GridView的代碼:

public class GridviewAdapter extends BaseAdapter { 
    private ArrayList<String> listImgUrl; 
    private ArrayList<String> listCost; 
    private ArrayList<String> listItem; 
    private ArrayList<String> listStore; 

    private Activity activity; 
// private Context context; 

    public GridviewAdapter(Context context) { 
     super(); 
    // this.context = context; 


    } 

    public void GridviewAdapterInit(Activity activity, ArrayList<String> listImgUrl, 
      ArrayList<String> listCost, ArrayList<String> listItem, 
      ArrayList<String> listStore) { 
     this.activity = activity; 

     this.listImgUrl = listImgUrl; 
     this.listCost = listCost; 
     this.listItem = listItem; 
     this.listStore = listStore; 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return listImgUrl.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     // TODO Auto-generated method stub 
     return listImgUrl.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public static class ViewHolder { 
     public ImageView imgViewFlag; 
     public TextView tvCost; 
     public TextView tvItem; 
     public TextView tvStore; 
    } 

    @SuppressLint("InflateParams") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     ViewHolder view; 
     LayoutInflater inflator = activity.getLayoutInflater(); 

     if (convertView == null) { 
      view = new ViewHolder(); 
      convertView = inflator.inflate(R.layout.gridview_row, null); 

      view.imgViewFlag = (ImageView) convertView 
        .findViewById(R.id.imageView1); 
      view.tvCost = (TextView) convertView.findViewById(R.id.tvCost); 
      view.tvItem = (TextView) convertView.findViewById(R.id.tvItem); 
      view.tvStore = (TextView) convertView.findViewById(R.id.tvStore); 



      convertView.setTag(view); 
     } else { 
      view = (ViewHolder) convertView.getTag(); 
     } 


      LoadImageFromURL(listImgUrl.get(position), view.imgViewFlag); 
      view.tvCost.setText(listCost.get(position)); 
      view.tvItem.setText(listItem.get(position)); 
      view.tvStore.setText(listStore.get(position)); 

     return convertView; 
    } 

    private void LoadImageFromURL(String url, ImageView imgView) 

    { 
     new ImageViewUpdator(imgView).execute(url); 

    } 

} 

利用上述代碼中,圖像被正確地裝載,如果圖像是6或更少。加載更多圖像時,會出現問題,例如圖像重複位置錯誤,很少圖像未加載。但是要加載的圖片數量沒問題。 不知道是什麼導致了這個問題。

感謝您的幫助

+0

這可能會幫助你http://stackoverflow.com/questions/21029223/loading-images-in-a-gridview-with -async-task-not-loading-correctly –

回答

1

如果要加載從URL圖像,那麼你需要使用picasso

畢加索是 Android的強大的圖像下載和緩存庫。

的代碼只需一行解決您的圖像加載問題:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
+0

問題不在於加載圖像從URL中顯示在GridView上。加載圖像正常工作。我會嘗試使用它,因爲它具有緩存功能。無論如何是什麼導致這個問題? – Ashok

+0

是上面的單行代碼是足以啓用緩存和加載圖像還是我需要添加更多? – Ashok

+0

@Ashok請參考畢加索 – SANAT