9er
9er

Reputation: 1734

Custom Android Adapter Never Showing Data

I have written a custom android adapter which isn't showing anything at all when added to a ListFragment. I know there are values in the List<Alarm> and that the database end is working correctly. Here is my custom adaptor

public class AlarmListAdapter extends ArrayAdapter<Alarm>{

public AlarmListAdapter(Context context, int resource, List<Alarm> alarms) {
    super(context, resource);
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    if(view == null){
        LayoutInflater viewInflator = LayoutInflater.from(getContext());
        view = viewInflator.inflate(R.layout.item_alarm_list, null);
    }else{
        Alarm alarm = getItem(position);
        if(alarm != null){
            TextView alarmName = (TextView) view.findViewById(R.id.alarm_list_item_name);
            TextView alarmTime = (TextView) view.findViewById(R.id.alarm_list_item_time);
            Switch status = (Switch) view.findViewById(R.id.alarm_list_item_status);
            alarmName.setText(alarm.getName());
            alarmTime.setText(alarm.getHour() + ":" + alarm.getMinute());
            //TODO set the status of the alarms
            status.setChecked(true);
        }
    }
    return view;
}   

}

Here is how I add the adaptor to the ListFragment.

List<Alarm> alarms = db.getAlarms();
AlarmListAdapter adapter = new AlarmListAdapter(getActivity(),
     R.layout.item_alarm_list, alarms);

The layout R.layout.item_alarm_list is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Switch
    android:id="@+id/alarm_list_item_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/alarm_list_item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

<TextView
    android:id="@+id/alarm_list_item_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

I am new to android and am hoping you guy's might help me understand whats going on here. Thanks in advance!

Upvotes: 1

Views: 58

Answers (2)

9er
9er

Reputation: 1734

I realized that I wasn't calling the right constructor in the super() call. it should be

super(context, resource, alarms);

Upvotes: 2

Shereef Marzouk
Shereef Marzouk

Reputation: 3392

Try this:

@Override
public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater viewInflator = LayoutInflater.from(getContext());
        view = viewInflator.inflate(R.layout.item_alarm_list, null);
    }
    Alarm alarm = getItem(position);
    if (alarm != null) {
        TextView alarmName = (TextView) view.findViewById(R.id.alarm_list_item_name);
        TextView alarmTime = (TextView) view.findViewById(R.id.alarm_list_item_time);
        Switch status = (Switch) view.findViewById(R.id.alarm_list_item_status);
        alarmName.setText(alarm.getName());
        alarmTime.setText(alarm.getHour() + ":" + alarm.getMinute());
        //TODO set the status of the alarms
        status.setChecked(true);
        return view;
    }
}

Upvotes: 2

Related Questions