2013-05-16 11 views

cevap

16

Context'u argümanlarından biri olarak alıp özel bir değişkende saklayan bir kurucu yapın.

public class SampleAdapter extends BaseAdapter { 
    private Context context; 

    public SampleAdapter(Context context) { 
     this.context = context; 
    } 

    /* ... other methods ... */ 
} 
+2

gibi bağlamını erişebilir Özellikle Android ile uğraşmaya başlamadan önce temel bilgiler. – Karakuri

+0

güzel yakalama ..... + 1 –

+0

İçeriği null olarak ayarlıyor. – Sid

24

Bir daha çözüm-

ebeveynlerinizden varsa, doğrudan sana Java üzerinde sağlam bir ele almak önermek yani-

public class SampleAdapter extends BaseAdapter { 

      LayoutInflater inflater; 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       if(inflater == null){ 
       Context context = parent.getContext(); 
       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       } 
       ... 
       ... 

       return convertView; 
      } 

    } 
+4

Doğru cevap bu olmalı. @ Karakuri'nin cevabı verimsizdir –

+3

Doğru cevap. –

3
Context context = parent.getContext();