gpt4 book ai didi

java - getView 未在自定义适配器中调用

转载 作者:行者123 更新时间:2023-11-29 03:24:22 24 4
gpt4 key购买 nike

我正在尝试在对话框中制作 ListView 。但是我的适配器中的 getView 函数永远不会被调用。有什么想法吗?

适配器:

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

设置适配器并制作对话框:

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

最佳答案

您将布局充气两次。对话框中的 ListView 与您设置的适配器不同。

修复它,例如改变

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

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

关于java - getView 未在自定义适配器中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com