Thursday, February 6, 2014

Custom List Dialog with Abstract callbacks for Android

ok this time I have decided to customize a List View Selection Prompt Dialog using an abstract class with two abstract functions which are invoked on selection event or on cancel event respectively. This method will simplifies the list view dialog to a level in which all you have to do is just to create an object for the class in the activity with necessary parameters and define actions what action to be taken when a callback is made to the abstract functions, I am not sure about the underlying faults that may can occur, please do share your thought on this.


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

Followers