0

When I debug the code, it is calling constructors in InviteListAdapter but it is not calling getView() method. I have tried multiple solutions available on stackoverflow but none of them works. Hope any one can find the mistake or solution for this.

invitableFriends.size()    

returns value greater than 0.

InviteListAdapter

public class inviteListAdapter extends ArrayAdapter<JSONObject> {

private final Context context;
private final List<JSONObject> invitableFriends;
private ImageView profilePicView;

public inviteListAdapter(Context context, List<JSONObject> invitableFriends) {
    super(context, R.layout.invite_adapter, invitableFriends);
    this.context = context;
    this.invitableFriends = invitableFriends;
}


@Override
public View getView(int position, View convertView, ViewGroup parent){


    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(context.LAYOUT_INFLATER_SERVICE);

    View listItemView = inflater.inflate(R.layout.invite_adapter, parent, false);

    profilePicView = (ImageView) listItemView.findViewById(R.id.inviteListItemProfilePic);
    TextView nameView = (TextView) listItemView.findViewById(R.id.inviteListItemName);

    JSONObject currentUser = invitableFriends.get(position);

    nameView.setText(currentUser.optString("first_name"));


    return listItemView;
}

 }

first.java

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

            return view;
}


final inviteListAdapter adapter = new inviteListAdapter(this,inviteFriendList);
    invitesGridView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
Keyul
  • 729
  • 1
  • 8
  • 20
  • 1
    are you sure `setAdapter` is called? when do you call this method? – Gennadii Saprykin Jul 29 '15 at 02:58
  • You have given `parent` as root and `false` as well, means you provide a parent and tell it not to take it as a parent? use `.inflate(R.layout.invite_adapter, null);` (no root, so it will take xml root as root) And of course, set your adapter inside `onCreateView` as that's where you are creating the view. – Darpan Jul 29 '15 at 03:19

3 Answers3

1
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

yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • Is it still work if I am passing inviteFriendList null? because I am getting data in it after onCreateView() for that reason I have call setAdapter out of onCreateView. – Keyul Jul 29 '15 at 23:45
  • No. It will not work. Post your complete code. in which method your are calling setAdapter. – yuva ツ Jul 30 '15 at 03:29
1

Here you have to use context class static method. Like, change context to Context.LAYOUT_INFLATER_SERVICE

LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Piyush
  • 18,895
  • 5
  • 32
  • 63
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

Its not good practice to create inflater object in getView() method, Instead create one in constructor using LayoutInflater.from(context);

This link will help you out How to use ArrayAdapter<myClass>

Community
  • 1
  • 1