我正在尝试将自己的AlertDialog与自定义列表视图一起使用,但似乎无法显示或运行而没有错误。
private void buildDialog(){
int selectedItem = -1; //somehow get your previously selected choice
LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.listview, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
builder.setTitle("Select Weapon").setCancelable(true);
builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
ListView lv = ((AlertDialog) dialog).getListView();
itemId = lv.getAdapter().getItemId(which);
new changeEQ().execute();
}
});
dialog = builder.create();
}
这是我的AlertDialog,但无法弄清楚要添加什么来获取我的自定义布局,listview&请使用listrow。我在网上浏览了导游,但他们所展示的内容似乎对我有用。 IE我必须做错事。
编辑:更改了代码以包含答案,但屏幕上显示的内容没有变化。没有错误但外观没有变化。
答案 0 :(得分:10)
如果您要将自定义布局传递给AlertDialog,请尝试:
LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
如果要定义侦听器,请尝试:
ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do as you please
}
});