自定义适配器列表不起作用

时间:2014-06-26 09:24:58

标签: android android-listview android-adapter

我有一个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 个答案:

答案 0 :(得分:1)

更改此

 super(context, R.layout.alarmlist_layout);

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

由于您有R.layout.alarmlist_layout,因此无需再次在getView中扩充布局。阅读下面的blackbelt评论。