Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

java - How to create an AlertDialog with ListView, without using AlertDialog.Builder?

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 Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...