簡體   English   中英

在帶有gridview的自定義適配器中未調用getView()方法

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

當我調試代碼時,它在InviteListAdapter中調用構造函數,但沒有在調用getView()方法。 我嘗試了在stackoverflow上可用的多種解決方案,但是它們都不起作用。 希望任何人都能找到錯誤或解決方案。

invitableFriends.size()    

返回大於0的值。

邀請列表適配器

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

 }

第一.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();
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

在這里,您必須使用上下文類的靜態方法。 像,將context更改為Context.LAYOUT_INFLATER_SERVICE

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

getView()方法中創建inflater對象不是一個好習慣,而是使用LayoutInflater.from(context);在構造函數中創建一個inflater對象LayoutInflater.from(context);

該鏈接將幫助您如何使用ArrayAdapter <myClass>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM