2014-06-26 48 views
0

我有一個ListView應該是其樣式已定義在我的自定義適配器中的列表。但是,當我打開ListActivity時,我得到一個空白的活動。列表不工作的自定義適配器

適配器代碼:

public class alarmListCustomAdapter extends ArrayAdapter<String> { 

    private final Context context; 
    private final String[] values; 
    Typeface Light; 

    public alarmListCustomAdapter(Context context, String[] values, String font) { 
     super(context, R.layout.alarmlist_layout); 
     this.context = context; 
     this.values = values; 
     Light = Typeface.createFromAsset(context.getAssets(), font); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.alarmlist_layout, parent, false); 

     TextView tV1 = (TextView) rowView.findViewById(R.id.alarm_time); 
     TextView tV2 = (TextView) rowView.findViewById(R.id.alarm_label); 
     tV2.setText(values[position]); 

     return rowView; 
    } 

} 

ListActivity代碼 -

public class alarmList extends ListActivity { 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
       "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
       "Linux", "OS/2" }; 

     // use your custom layout 
     alarmListCustomAdapter adapter = new alarmListCustomAdapter(this, values, "fonts/Lato-Light.ttf"); 
     setListAdapter(adapter); 

    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     String item = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    } 

} 

我沒有收到我的logcat的任何警告或錯誤。什麼可能會出錯?

我試圖通過here瞭解自定義適配器的實現。

回答

1

更改此

super(context, R.layout.alarmlist_layout); 

super(context, R.layout.alarmlist_layout,values); 

既然你有R.layout.alarmlist_layout沒有必要在getView再次膨脹的佈局。閱讀下面的黑帶評論。

+1

你也可以建議他刪除視圖膨脹的邏輯,因爲超級會去照顧它。 – Blackbelt

+0

@blackbelt是的,我一直忘記那一點,你指出了以前的一個類似的職位 – Raghunandan

+0

@Raghunandan如何刪除充氣機?如果沒有它,'findViewbyID'似乎不起作用。 –

相關問題