Usage (inside an activity) :
new ListDialogCustom(this,arrayAdapter,"Select A Table : ") { @Override public void onSelect(DialogInterface dialog,String text, int which) { // TODO Auto-generated method stub dialog.dismiss(); } @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); } };
If you want to change a UI or Activity component inside the abstract method please don't forget to use :
//to Update UI Componenets runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub } });
Class ListDialogCustom.java
basically this class creates a normal list view dialog and populate items in it with the help of a string based ArrayAdapter :
package com.example.android; import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences.Editor; import android.util.Log; import android.widget.ArrayAdapter; public abstract class ListDialogCustom { public ListDialogCustom(Context context,final ArrayAdapter<String> arrayAdapter,String prompt) { AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); builderSingle.setIcon(R.drawable.ic_launcher); builderSingle.setTitle(prompt); // final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.select_dialog_singlechoice); builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { onCancel(dialog); // dialog.dismiss(); } }); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final String name_id= arrayAdapter.getItem(which); onSelect(dialog,name_id,which); dialog.dismiss(); } }); builderSingle.show(); } public abstract void onSelect(DialogInterface dialog,String text,int which); public abstract void onCancel(DialogInterface dialog); }
No comments:
Post a Comment