AlertDialog的自定义列表视图 [英] Custom listview for AlertDialog
问题描述
我正在尝试使AlertDialog具有自定义列表视图,但似乎无法正确显示或运行它.
I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.
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浏览器我一定做错了事.
This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.
已更改代码以包括答案,但屏幕上显示的内容没有变化.没有错误,外观也没有变化.
changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.
推荐答案
如果您有想要传递给AlertDialog的自定义布局,请尝试:
If you have a custom layout that you want to pass to your AlertDialog try:
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);
如果要定义侦听器,请尝试:
If you want to define listeners try:
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
}
});
这篇关于AlertDialog的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!