Custom adapter getview is not called
Asked Answered
D

5

12

I have a Custom adapter with a ListFragment but the adapters getView() is not getting called at all.

This is how the adapter looks like -

public class ModuleListItemAdapter extends BaseAdapter {

    List<ModuleItem> list;
    Context context;
    Module mod;

    public ModuleListItemAdapter() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ModuleListItemAdapter(Context context, List<ModuleItem> list, Module mod) {
        super();
        this.list = list;
        this.context = context;
        this.mod = mod;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.moduleitem, null);
        GenerateModuleItemView gmiv = new GenerateModuleItemView(context);
        rl.addView(gmiv.itemDispView(mod.getFields(), list.get(position)));
        return rl;
    }

    public void setValue(List<ModuleItem> l) {
        this.list = l;
        notifyDataSetChanged();
    }

    public void addValue(ModuleItem item) {
        this.list.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

And the Fragment -

public class ModuleItemListFragment extends ListFragment {

    List<ModuleItem> list;
    Module mod;

    public ModuleItemListFragment() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ModuleItemListFragment(List<ModuleItem> list, Module mod) {
        super();
        this.list = list;
        this.mod = mod;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.list, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ModuleListItemAdapter adapter = new ModuleListItemAdapter(getActivity(), list, mod);
        setListAdapter(adapter);
    }
}

And this is how my layout looks like -

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:divider="#000000"
          android:dividerHeight="1dip" />

I don't know what the problem is here.

Dupe answered 27/4, 2013 at 9:8 Comment(3)
@Override public int getCount() { return list.size(); } . Please use google before asking a question.Demagnetize
As Luksprog said, you need to add something to getCount. It's not going to try and create any views if it thinks you want 0.Gault
Are you sure your "list" is not empty?Engedus
E
35

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.

Engedus answered 27/4, 2013 at 9:16 Comment(2)
Thanks a lot..:)lessons learned mistakes happen along the way..:PDupe
For me I was using addAll method but have not overridden to update the list, hence count was always 0.Adiaphorism
I
2

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.

Ignatz answered 22/5, 2016 at 21:18 Comment(0)
Z
0

I have same problem, in my case i did't initialized my local array list in my Fragment constructor.

ArrayList<Image> mImageList;

public ImageCategoryAdapter(Context context, ArrayList<Image> itemList) {
    super(context, 0);

    mImageList = itemList;
}

I missed the mImageList = itemList; part.

It is only a man made error but it might be helpful to someone facing the same issue.

Zaidazailer answered 13/1, 2016 at 3:4 Comment(0)
D
0

getView() method is calling when the data set has at least one record to display in the view.

getView() : Returns: A View corresponding to the data at the specified position.

And if we the size of data set is 0 therefore, the position is also 0.

As a result getView() method is not calling.

Deltadeltaic answered 20/4, 2016 at 16:5 Comment(0)
S
0

I had this problem and in my case, the problem was that I mistakenly created 2 adapters. One of them I was passing to setAdapter and the other one I was using to add items to using add. So the adapter that mattered was never being added to.

Sibbie answered 24/4, 2016 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.