krtkush
krtkush

Reputation: 1528

Custom adapter for list not working

I have a ListView which is supposed to be lists whose style has been defined in my custom adapter. But, when I open the ListActivity, I get a blank activity.

Adapter code:

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 code-

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();
    }

}

I'm not getting any warnings or error in my LogCat. What can be going wrong?

I'm trying to learn the custom adapter implementation through here.

Upvotes: 0

Views: 57

Answers (1)

Raghunandan
Raghunandan

Reputation: 133580

Change this

 super(context, R.layout.alarmlist_layout);

to

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

Since you have R.layout.alarmlist_layout there is no need to inflate the layout again in getView. Read blackbelt's comment below.

Upvotes: 1

Related Questions