简体   繁体   English

自定义适配器中未调用getView

[英]getView not getting called in custom adapter

I am trying to make a listview within a dialog. 我试图在对话框中创建一个列表视图。 But my getView function in my adapter is never getting called. 但是我的适配器中的getView函数永远不会被调用。 Any ideas? 有任何想法吗?

Adapter : 适配器:

public class DialogAdapter extends BaseAdapter{

HashMap<String, String> extraInfo;
private String[] mKeys;

Context mContext;
int res;
LayoutInflater vi;
public DialogAdapter(){

}

public DialogAdapter(Context context,HashMap<String, String> extraInfo) {
    this.extraInfo = extraInfo;
    mKeys = this.extraInfo.keySet().toArray(new String[this.extraInfo.size()]);
    mContext = context;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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


@Override
public Object getItem(int position) {
    return extraInfo.get(mKeys[position]);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Textholder textholder = null;
    System.out.println("Test");
    if(convertView ==null){
        convertView = vi.inflate(R.layout.cust_dialog_item, parent, false);
        textholder = new Textholder();
        textholder.info = (TextView)convertView.findViewById(R.id.textProfile);
        textholder.value = (TextView)convertView.findViewById(R.id.valueProfile);

        convertView.setTag(textholder);
    }
    else{
        textholder = (Textholder)convertView.getTag();
    }
    textholder.info.setText(mKeys[position]);
    textholder.value.setText(getItem(position).toString());




    return convertView;
}

private static class Textholder {
    TextView info;
    TextView value;
}

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

} }

Setting the adapter and making the dialog : 设置适配器并进行对话框:

        DialogAdapter dAdapter = new DialogAdapter(x,extraInfo);
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
        .setView(getActivity().getLayoutInflater().inflate(R.layout.custdialog, null))
        .create();
        View x = inflater.inflate(R.layout.custdialog, container, false);
        ListView custListDialog = (ListView)x.findViewById(android.R.id.list);

        dialog.show();
        custListDialog.setAdapter(dAdapter);

You're inflating the layout twice. 您要扩大布局两次。 The listview in the dialog is not the same you set the adapter to. 对话框中的列表视图与您将适配器设置为不同。

To fix it, eg change 要修复它,例如更改

AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setView(getActivity().getLayoutInflater().inflate(R.layout.custdialog, null))
.create();
View x = inflater.inflate(R.layout.custdialog, container, false);

to

View x = inflater.inflate(R.layout.custdialog, container, false);
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setView(x)
.create();

Try this 尝试这个

   custListDialog.setAdapter(dAdapter);
 dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM