Phone: 406-788-9165

Android Java : List Selector Dialog class

This is a simple List Selector Dialog class to create simple pop-up lists in Android. It can handle 2 lists that represent keys and values, both of which are sent back to the calling interface. This class does not handle multiple item selection.

This class can handle Standard Java String[] arrays, ArrayLists, or HashMaps

The dialog class: Name this ListSelectorDialog.java. put it in the src/ directory of your project.

Example:

package com.yourpackage;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;

public class ListSelectorDialog {
	Context context;
	Builder adb;
	String title;

	// our interface so we can return the selected key/item pair.
	public interface listSelectorInterface {
		void selectedItem(String key, String item);
		void selectorCanceled();
	}
	
	ListSelectorDialog(Context c) {
		this.context = c;
	}
	ListSelectorDialog(Context c, String newTitle) {
		this.context = c;
		this.title = newTitle;
	}
	
	ListSelectorDialog setTitle(String newTitle) {
		this.title = newTitle;
		return this;
	}
	
	ListSelectorDialog show(ArrayList il, final listSelectorInterface di) {
		String l[] = null;
		l = il.toArray(new String[il.size()]);
		
		show(l, l, di);
		return this;
	}
	
	ListSelectorDialog show(ArrayList il, ArrayList ik, 
	  final listSelectorInterface di) {
		// convert the ArrayList's to standard Java arrays.
		String l[] = null; String k[] = null;
		l = il.toArray(new String[il.size()]);
		k = ik.toArray(new String[ik.size()]);
		
		show(l, k, di);
		return this;
	}
	
	ListSelectorDialog show(HashMap hashmap, final listSelectorInterface di) {
		// convert the hashmap to lists
		String[] il = new String[hashmap.size()];
		String[] ik = new String[hashmap.size()];
		// HashMap iteration
		int i = 0;
		for (Object key: hashmap.keySet()) {
			il[i] = key.toString();
			ik[i] = hashmap.get(key).toString();
			i++;
		}
		// now show the selection dialog
		show(il, ik, di);
		return this;
	}
	
	ListSelectorDialog show(final String[] itemList, final listSelectorInterface di) {
		// if only 1 list supplied, the list serves as both keys and values.
		show(itemList, itemList, di);
		return this;
	}
	
	ListSelectorDialog show(final String[] itemList, final String[] keyList, 
	  final listSelectorInterface di) {
		// set up the dialog
		adb = new AlertDialog.Builder(context);
		adb.setCancelable(false);
		adb.setItems(itemList, new DialogInterface.OnClickListener() {
	        // when an item is clicked, notify our interface
	    	public void onClick(DialogInterface d, int n) {
				d.dismiss();
				di.selectedItem(keyList[n], itemList[n]);
	        }
		});
		adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			// when user clicks cancel, notify our interface
			public void onClick(DialogInterface d, int n) {
				d.dismiss();
				di.selectorCanceled();
			}
		});
		adb.setTitle(title);
		// show the dialog
		adb.show();
		return this;
	}
}

Example of how to call the ListSelectorDialog and handle selected item input:

Example:

// create an instance of ListSelectorDialog.
ListSelectorDialog dlg = new ListSelectorDialog(this, "Select an Operator");
// create our arrays of keys and values to send to the dialog.
String[] listk = new String[] {"+", "-", "*", "/", "%"};
String[] listv = new String[] {"+ Plus", "- Minus", "* Multiply", "/ Divide", "% Modulus"};
// show the list dialog.
dlg.show(listv, listk, new ListSelectorDialog.listSelectorInterface() {
	// procedure if user cancels the dialog.
	public void selectorCanceled() {
		Toast.makeText(getApplicationContext(), 
		  "User Canceled the request!", 1).show();
	}
	// procedure for when a user selects an item in the dialog.
	public void selectedItem(String key, String item) {
		Toast.makeText(getApplicationContext(), 
		  "User Has selected item '"+item+"' having key '"+key+"'!", 1).show();
	}
});

Android List Select Example!



Published by: Thomas Penwell
Initially published on: July 25, 2014
Article last modified on: Sunday, January 31, 2016.

Home - Web Portfolio - Web site Tools - Database Design Service - Consulting
Web Design Service - Web Hosting Solutions - Service Rates
Client Login - Feedback Form - Promotions - Partners - Articles - Perl Scripts


Our Sites:
restaurantorderpad.com