Custom Adapter Getview() Method Is Not Called

Custom Adapter getView() method is not called

The only reasons getView is not called are:

  1. getCount returns 0.
  2. you forget to call setAdapter on the ListView.
  3. If the ListView's visibility (or its container's visibility) is GONE. Thanks to @TaynãBonaldo for the valuable input.
  4. ListView is not attached to any viewport layout. That is, mListView = new ListView(...) is used without myLayout.addView(mListView).

In the onPostExcute, after you create a new instance of CarListAdapter I will suggest you to update the new instance to your ListView. Indeed you need to call again

 mList.setAdapter(adapter);

Edit: setAdapter should be always called on the ui thread, to avoid unexpected behaviours

Edit2:

The same applies to RecyclerView. Make sure that

  • getItemCount is returning a value grater than 0 (usually the dataset size)
  • both setLayoutManager and setAdapter have to be called on the UI Thread
  • The visibility of the widget has to be set to VISIBLE

Custom adapter getview is not called

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.

The getView() method of ArrayAdapter is not getting called

You just forgot to provide the data to the constructor and then make the correct call within the constructor:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
super(context,resource,textViewResourceId, items);
this.context1 = context;
// TODO Auto-generated constructor stub
}

getView() method is not called in custom adapter with gridview

public View onCreateView (
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_first,container, false);

invitesGridView = (GridView)view.findViewById(R.id.invitesGridView);
final inviteListAdapter adapter = new inviteListAdapter(this,inviteFriendList);
invitesGridView.setAdapter(adapter);
adapter.notifyDataSetChanged();

return view;
}

Call setAdapter inside onCreateView

getView not called on baseAdapter class

In your method

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

you are returning 0. so your method is not called. Instead return size of your list-view in getCount method.

Getview is not being called in custom adapter I debugged and I found position was being returned as -1

After seeing code I can suggest to you ,
if you'r using AsyncTask then you should update your UI in onPostExecute() method. So set your custom adapter in that method.

 @Override
protected void onPostExecute(String result) {
cst= new CustomAdapter(this, objects, prgmImages);
lv.setAdapter(cst);
}

Also in custom adapter use ViewHolder pattern in getView() method which will prevent the duplication items while scrolling listView.



Related Topics



Leave a reply



Submit