在使用gridview的自定义适配器中不调用getView()方法

时间:2015-07-29 02:51:51

标签: java android gridview

当我调试代码时,它调用InviteListAdapter中的构造函数,但它不调用getView()方法。我已尝试在stackoverflow上提供多种解决方案,但它们都不起作用。希望任何人都能找到错误或解决方案。

invitableFriends.size()    

返回大于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();

3 个答案:

答案 0 :(得分: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;
 }

在onCreateView

中调用setAdapter

答案 1 :(得分:1)

这里你必须使用context类静态方法。比如,将context更改为Context.LAYOUT_INFLATER_SERVICE

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

答案 2 :(得分:0)

inflater方法中创建getView()对象不是一个好习惯,而是使用LayoutInflater.from(context);在构造函数中创建一个

此链接可以帮助您 How to use ArrayAdapter<myClass>