2016-01-21 67 views
-1

這裏是我的代碼來加載圖像從服務器...有什麼不對? 應用程序無法加載任何東西,並顯示了強制停止error.I的代碼中刪除的部分,我敢肯定,這就是工作...爲什麼我的代碼無法加載任何圖像?

public class MenuAdapter extends BaseAdapter{ 

    private LayoutInflater myInflater; 


    private List<String> labelIDs = new ArrayList<String>(); 

    private List<String> imageIDs = new ArrayList<String>(); 
    public void setData(List<String> values, List<String> images){ 
     this.labelIDs = values; 
     this.imageIDs = images; 
    } 

    public View getView(int position, View convertView, ViewGroup parent){ 
     ViewHolder holder; 

     try { 
      URL url = new URL("url"+imageIDs.get(position)); 
      Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

      holder.ivIcon.setImageBitmap(bmp); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     holder.tvLabel.setText(labelIDs.get(position)); 
     return convertView; 
    } 

    static class ViewHolder { 
     ImageView ivIcon; 
     TextView tvLabel; 
    } 
} 
+0

你是什麼意思?那是對的。你收到的錯誤是什麼? – TejjD

回答

0

但我不知道我怎麼能取代上下文與正確的術語?

可以使用thisYourActivityName.this如果你繼承Activity。如果您繼承Fragment,則可以使用getActivity()。如果你在一個沒有直接訪問Context對象的類中,你可以通過它,或者在你的情況下,你可以使用holder.ivIcon.getContext()

+1

tanx ...我明白了:) –

+0

但我目前還無法加載任何圖像...。 –

0

上下文表示您的Activity的對象。Activity從Context Class繼承。

相關問題