如何创建ListView的一个AlertDialog,而无需使用AlertDialog.Builder?ListView、AlertDialog、Builder

2023-09-05 03:29:49 作者:潇洒丶疯一回

我有 AlertDialog 的子类,应该显示所有范围内的可用无线网络的列表。

I have a subclass of AlertDialog that should display a list of all the available Wifi networks in range.

我要那个对话框本身将负责启动无线扫描和接收的结果。

I want that the dialog itself will be responsible for initiating Wifi scan and receiving the results.

由于这个原因,我不能使用 AlertDialog.Builder 设置的ListView 的项目,因为在那一刻创建对话框我没有他们呢,他们可能会在presentation改变。

For this reason I cannot use the AlertDialog.Builder to set the ListView items, because at the moment of creating the dialog I don't have them yet, and they might change during presentation.

那么,我问的是我怎么可以使用内置的 AlertDialog 支持present一个选项列表,而不 AlertDialog.Builder

So what I'm asking is how can I use the built-in support for AlertDialog to present a single choice list, without the AlertDialog.Builder?

如果这是不可能的,我怎么创建我自己的ListView并将其设置为对话内容的看法?

If it is impossible, how do I create my own ListView and set it as the content view for the dialog?

推荐答案

见下文code:

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>