5

我试图让我的 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 但无法弄清楚要添加什么来获得我的自定义布局、列表视图和列表行。我在网上查看了指南,但它们显示的内容似乎对我没有用。IE 我一定做错了什么。

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

4

1 回答 1

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

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

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
    }
});
于 2012-04-28T22:16:43.443 回答