Java – How do I create an AlertDialog using a ListView without using AlertDialog.Builder?

How do I create an AlertDialog using a ListView without using AlertDialog.Builder?… here is a solution to the problem.

How do I create an AlertDialog using a ListView without using AlertDialog.Builder?

I have a subclass of AlertDialog, which should display a list of all available Wifi networks within range.

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

For this reason, I can’t use AlertDialog.Builder to set up ListView items because I didn’t have them when I created the dialog, and they might change during the presentation.

So what I’d like to ask is, without AlertDialog.Builder, how can I use the built-in support for AlertDialog to render a single picklist?

If that’s not possible, how do I create my own ListView and set it as the contents of the dialog View?

Solution

Look at the code below:

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>

Related Problems and Solutions