gpt4 book ai didi

android - AlertDialog 的自定义 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:35 25 4
gpt4 key购买 nike

我试图让我的 AlertDialog 具有自定义 ListView ,但似乎无法正确显示或运行它。

   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 和列表行。我在网上四处查看指南,但他们显示的任何内容似乎都不适合我。 IE 我一定是做错了什么。

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

最佳答案

如果您有自定义布局并想传递给 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
}
});

关于android - AlertDialog 的自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367934/

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