AlertDialog的自定义列表视图

mae*_*ebe 5 android listview android-alertdialog

我正在尝试使用自定义列表视图的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();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的AlertDialog,但无法弄清楚要添加什么来获取我的自定义布局,listview和listrow.我在网上浏览了导游,但他们所展示的内容似乎对我有用.IE我必须做错事.

编辑:更改了代码以包含答案,但屏幕上显示的内容没有变化.没有错误,但外观没有变化.

Sam*_*Sam 11

如果您要将自定义布局传递给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);
Run Code Online (Sandbox Code Playgroud)

如果要定义侦听器,请尝试:

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
    }
});
Run Code Online (Sandbox Code Playgroud)