2012-05-21 14 views
5

मैं कस्टम एडाप्टर बनाने के साथ अटक गया हूं। मैं ListView के अंदर बटन पर ऑनक्लिक लिस्टनर सेट करना चाहता हूं और मुझे यह विषय मिला जो ठीक है how-to-setonclicklistener-on-the-button-inside-the-listview लेकिन समस्या यह है कि मुझे GetLayoutInflater लाइन पर पहुंचने योग्य कोड त्रुटि मिल रही है।कस्टम सरल के अंदर getLayoutInflaterCursorAdapter

यहाँ काम नहीं करता है मेरी कोड

public class MyCursorAdapter extends SimpleCursorAdapter{ 

    private final Context ctx; 
    private Button tagButton = null; 

    public MyCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     ctx = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return super.getView(position, convertView, parent); 
     LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = li.inflate(R.layout.tags_list_element, null, true); 
     tagButton= (Button)rowView.findViewById(R.id.tag_title); 
     tagButton.setTag(position); 

     tagButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
      } 
     }); 
     return rowView; 

    } 

} 

दोनों तरीकों है मुझे

LayoutInflater inflater = context.getLayoutInflater(); 

और

LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
+0

u plz कर सकते हैं मुझे दिखाओ त्रुटि लॉग ........... – c2dm

उत्तर

11

प्रयास करें:

View myView = LayoutInflater.from(ctx).inflate(R.layout.my_view, null); 

इसके अलावा, आपको क्या अपवाद मिलता है?


संपादित करें: अपने "getView" विधि में, पहली पंक्ति है "वापसी .....", तो अपने विधि के बाकी कभी क्रियान्वित किया जा नहीं होगा, मुझे लगता है कि ....;)

+0

ये कि मैंने शुरू .. धन्यवाद पर वापसी याद किया;) – Greg

2

देखने के एक प्रदर्शन बिंदु से:

View myView = LayoutInflater.from(context).inflate(R.layout.my_view, parent, false); 

सही है; लेकिन एडाप्टर के अंदर एक अंतिम क्षेत्र में inflater स्टोर करने के लिए यह और अधिक कुशल है।

private final Context ctx; 
private final LayoutInflater mInflater; 
private Button tagButton = null; 

public MyCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
    super(context, layout, c, from, to); 
    ctx = context; 
    mInflater = LayoutInflater.from(ctx); 
} 

फिर अपना getView ऑपरेशन करें।

//.... 
final View v = mInflater.inflate(R.layout.list_item, parent, false); 
//.... 
//stuff here 
// 
return v; 

ALSO सुनिश्चित करें कि आप अपने गतिविधि संदर्भ हैं, तो आप गलत संदर्भ का उपयोग करते समय थिंगिंग समस्याएं प्राप्त करेंगे।

सादर, क्रिस

संबंधित मुद्दे