Custom adapter getview is not called

I solved this problem cleanly by reading the ArrayAdapter code. Apparently it keeps reference to an internal list of objects. All I had to do was to pass my ArrayList pointer to super constructor thus:

public FriendsAdapter(Context context, int resource, ArrayList<FriendItem> friends) {
    super(context, resource, friends);
} 

and I didn't have to override any getItem or getCount methods.

Note: This is for ArrayAdapter and API level 23+ so it may not apply to you.


It's because getCount() returns zero. The number you return in getCount() is the times the getView() will be called. In getCount() you should always return the size of the list.

Therefore

@Override
public int getCount() {
    return list.size();
}

@Override
public ModuleItem getItem(int position) {
    return list.get(position);
}

Also, maybe the layout's ListView id is not android.R.id.list?

Make sure you have in xml

<ListView
    android:id="@android:id/list"
    ...

Also, don't ever pass any data to a fragment in constructor.

WRONG:

public ModuleItemListFragment(List<ModuleItem> list,Module mod) {
    super();
    this.list=list;
    this.mod=mod;
}

RIGHT:

private static final String EXTRA_LIST = "ModuleItemListFragment.EXTRA_LIST";
private static final String EXTRA_MODULE = "ModuleItemListFragment.EXTRA_MODULE";

public static ModuleItemListFragment instantiate(ArrayList<ModuleItem> list, Module mod) {
    final Bundle args = new Bundle();
    args.putParcelable(EXTRA_LIST, list);
    args.putParcelable(EXTRA_MODULE, module);

    final ModuleItemListFragment f = new ModuleItemListFragment();
    f.setArguments(args);
    return f;
}

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    final Bundle args = getArguments();
    this.list = args.getParcelable(EXTRA_LIST);
    this.module = args.getParcelable(EXTRA_MODULE);
}

Of course, you have to make your Module Parcelable or Serializable.

You must specify args because the Fragment can be killed and restored by the system and if you pass data via setters or constructors they will not be restored and therefore can become null in some circumstances.