How to create an AlertDialog with a ListView without using AlertDialog.Builder?

I have a subclass AlertDialogthat should list all available Wi-Fi networks in a range.

I want the dialogue itself to be responsible for starting the Wifi scan and getting the results.

For this reason, I can not use AlertDialog.Builderto install elements ListView, because at the time of creating the dialogue I do not have them yet, and they can change during the presentation.

So I ask, how can I use the built-in support AlertDialogto present a single select list without AlertDialog.Builder?

If this is not possible, how can I create my own ListView and set it as a content view for the dialog?

+3
source share
2 answers

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>
+4
source

you can choose one dialogue

 final Dialog _dialog1 = new Dialog(this);
  _dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
   _dialog1.setContentView(R.layout.your_own_listView);

in setContentView() u can put ur own listView
0
source

All Articles