如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog? [英] How to create an AlertDialog with ListView, without using AlertDialog.Builder?

查看:31
本文介绍了如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

解决方案

见下面代码:

public void show_alert() {//TODO 自动生成的方法存根final Dialog dia = new Dialog(this);dia.setContentView(R.layout.alert);dia.setTitle("选择要导入的文件");dia.setCancelable(true);list_alert = (ListView) dia.findViewById(R.id.alert_list);list_alert.setAdapter(new ArrayAdapter(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,长 arg3) {String fname = main_genral_class.file_list.get(pos);dia.dismiss();}});dia.show();}

布局文件名alert.xml

<列表视图android:id="@+id/alert_list"android:layout_width="fill_parent"android:layout_height="wrap_content" ></ListView></LinearLayout>

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.

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.

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?

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

解决方案

see below 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();
}

layout file name 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>

这篇关于如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆