gpt4 book ai didi

java - 如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog?

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:15 27 4
gpt4 key购买 nike

我有一个 AlertDialog 的子类,它应该显示范围内所有可用 Wifi 网络的列表。

我希望对话框本身负责启动 Wifi 扫描并接收结果。

出于这个原因,我不能使用 AlertDialog.Builder 来设置 ListView 项,因为在创建对话框时我还没有它们,并且它们可能会在演示过程中发生变化。

所以我想问的是,如果没有 AlertDialog.Builder,我如何使用对 AlertDialog 的内置支持来呈现单个选择列表?

如果不可能,我该如何创建自己的 ListView 并将其设置为对话框的内容 View ?

最佳答案

看下面代码:

public void show_alert() {
// TODO Auto-generated method stub

final Dialog dia = new Dialog(this);
dia.setContentView(R.layout.alert);
dia.setTitle("Select File to import");
dia.setCancelable(true);

list_alert = (ListView) dia.findViewById(R.id.alert_list);
list_alert.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
main_genral_class.file_list));
list_alert.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
String fname = main_genral_class.file_list.get(pos);
dia.dismiss();

}
});
dia.show();
}

布局文件名alert.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/alert_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

关于java - 如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652688/

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