For implementing a ListView, we first create a xml which contains a ListView named list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Next we create a Dialog Object and inflate the above xml and when the listItem is clicked then a Alert Dialog windows comes
The java file is listed below
import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class DialoglistView extends Activity implements OnItemClickListener{ /** Called when the activity is first created. */ String[] val = {"sunday","monday","tuesday","thrusday","friday","wednesday","march"}; ListView list; Dialog listDialog; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); showdialog(); } private void showdialog() { listDialog = new Dialog(this); listDialog.setTitle("Select Item"); LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = li.inflate(R.layout.list, null, false); listDialog.setContentView(v); listDialog.setCancelable(true); //there are a lot of settings, for dialog, check them all out! ListView list1 = (ListView) listDialog.findViewById(R.id.listview); list1.setOnItemClickListener(this); list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, val)); //now that the dialog is set up, it's time to show it listDialog.show(); } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Delete item "+arg2) .setPositiveButton("OK ", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { System.out.println("OK CLICKED"); } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); listDialog.cancel(); } }); AlertDialog alert = builder.create(); alert.setTitle("Information"); alert.show(); } }
Why is this site loaded with “missing picture” again and again???
Sorry Susan Actually I lost those images when my site was done initially.
Pingback: Android dialog with ListView » Ideal Codes
Pingback: Show list perspective in a popup window – Android | Zenzen
Many thanks for this example.