diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2012-04-15 07:49:07 +0200 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2012-04-15 07:49:07 +0200 |
commit | 88675c29ffb74c745ddc8c459ff5256d060e49c9 (patch) | |
tree | 470c6efcc143a1138481756367072923843521d9 /main/src/cgeo | |
parent | 4a7be787698608af487dbe4ae124d5bb541eb84c (diff) | |
download | cgeo-88675c29ffb74c745ddc8c459ff5256d060e49c9.zip cgeo-88675c29ffb74c745ddc8c459ff5256d060e49c9.tar.gz cgeo-88675c29ffb74c745ddc8c459ff5256d060e49c9.tar.bz2 |
refactoring: simplify list based alert dialogs
Diffstat (limited to 'main/src/cgeo')
4 files changed, 81 insertions, 63 deletions
diff --git a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java index ec7dd0f..052189a 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java @@ -19,10 +19,7 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; import android.widget.ArrayAdapter; -import android.widget.TextView; import java.util.ArrayList; import java.util.List; @@ -66,6 +63,16 @@ public final class NavigationAppFactory extends AbstractAppFactory { * The id - used in c:geo settings */ public final int id; + + /* + * display app name in array adapter + * + * @see java.lang.Enum#toString() + */ + @Override + public String toString() { + return app.getName(); + } } /** @@ -127,24 +134,9 @@ public final class NavigationAppFactory extends AbstractAppFactory { } /* * Using an ArrayAdapter with list of NavigationAppsEnum items avoids - * handling between mapping list positions allows us to do dynamic filtering of the list based on usecase. + * handling between mapping list positions allows us to do dynamic filtering of the list based on use case. */ - final ArrayAdapter<NavigationAppsEnum> adapter = new ArrayAdapter<NavigationAppsEnum>(activity, android.R.layout.select_dialog_item, items) { - @Override - public View getView(int position, View convertView, ViewGroup parent) { - TextView textView = (TextView) super.getView(position, convertView, parent); - textView.setText(getItem(position).app.getName()); - return textView; - } - - @Override - public View getDropDownView(int position, View convertView, ViewGroup parent) { - TextView textView = (TextView) super.getDropDownView(position, convertView, parent); - textView.setText(getItem(position).app.getName()); - return textView; - } - }; - adapter.setDropDownViewResource(android.R.layout.select_dialog_item); + final ArrayAdapter<NavigationAppsEnum> adapter = new ArrayAdapter<NavigationAppsEnum>(activity, android.R.layout.select_dialog_item, items); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { @@ -298,7 +290,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** * Starts the second default navigation tool if correctly set and installed or the compass app as default fallback. - * + * * @param geo * @param activity * @param cache @@ -321,7 +313,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** * Returns the default navigation tool if correctly set and installed or the compass app as default fallback - * + * * @param activity * @return never <code>null</code> */ @@ -341,7 +333,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** * Returns the second default navigation tool if correctly set and installed or the compass app as default fallback - * + * * @param activity * @return never <code>null</code> */ diff --git a/main/src/cgeo/geocaching/export/ExportFactory.java b/main/src/cgeo/geocaching/export/ExportFactory.java index c4fdf36..8b3df58 100644 --- a/main/src/cgeo/geocaching/export/ExportFactory.java +++ b/main/src/cgeo/geocaching/export/ExportFactory.java @@ -52,7 +52,6 @@ public abstract class ExportFactory { } final ArrayAdapter<Export> adapter = new ArrayAdapter<Export>(activity, android.R.layout.select_dialog_item, export); - adapter.setDropDownViewResource(android.R.layout.select_dialog_item); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { diff --git a/main/src/cgeo/geocaching/filter/AbstractFilter.java b/main/src/cgeo/geocaching/filter/AbstractFilter.java index 49cf84a..e9f9003 100644 --- a/main/src/cgeo/geocaching/filter/AbstractFilter.java +++ b/main/src/cgeo/geocaching/filter/AbstractFilter.java @@ -26,4 +26,14 @@ abstract class AbstractFilter implements IFilter { public String getName() { return name; } + + /* + * show name in array adapter + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return getName(); + } } diff --git a/main/src/cgeo/geocaching/filter/FilterUserInterface.java b/main/src/cgeo/geocaching/filter/FilterUserInterface.java index 271df9d..1d22e52 100644 --- a/main/src/cgeo/geocaching/filter/FilterUserInterface.java +++ b/main/src/cgeo/geocaching/filter/FilterUserInterface.java @@ -12,65 +12,85 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.res.Resources; +import android.widget.ArrayAdapter; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; -import java.util.Map.Entry; +import java.util.Comparator; public final class FilterUserInterface { + private static class FactoryEntry { + private final String name; + private final Class<? extends IFilterFactory> filterFactory; + + public FactoryEntry(final String name, final Class<? extends IFilterFactory> filterFactory) { + this.name = name; + this.filterFactory = filterFactory; + } + + @Override + public String toString() { + return name; + } + } + private final IAbstractActivity activity; - private final HashMap<Integer, Class<? extends IFilterFactory>> registry; + private final ArrayList<FactoryEntry> registry; private final Resources res; public FilterUserInterface(final IAbstractActivity activity) { this.activity = activity; this.res = cgeoapplication.getInstance().getResources(); - registry = new HashMap<Integer, Class<? extends IFilterFactory>>(); + registry = new ArrayList<FactoryEntry>(); if (Settings.getCacheType() == CacheType.ALL) { - registry.put(R.string.caches_filter_type, TypeFilter.Factory.class); + register(R.string.caches_filter_type, TypeFilter.Factory.class); } - registry.put(R.string.caches_filter_size, SizeFilter.Factory.class); - registry.put(R.string.cache_terrain, TerrainFilter.Factory.class); - registry.put(R.string.cache_difficulty, DifficultyFilter.Factory.class); - registry.put(R.string.cache_attributes, AttributeFilter.Factory.class); - registry.put(R.string.cache_status, StateFilter.Factory.class); - registry.put(R.string.caches_filter_track, TrackablesFilter.class); - registry.put(R.string.caches_filter_modified, ModifiedFilter.class); + register(R.string.caches_filter_size, SizeFilter.Factory.class); + register(R.string.cache_terrain, TerrainFilter.Factory.class); + register(R.string.cache_difficulty, DifficultyFilter.Factory.class); + register(R.string.cache_attributes, AttributeFilter.Factory.class); + register(R.string.cache_status, StateFilter.Factory.class); + register(R.string.caches_filter_track, TrackablesFilter.class); + register(R.string.caches_filter_modified, ModifiedFilter.class); + + // sort by localized names + Collections.sort(registry, new Comparator<FactoryEntry>() { + + @Override + public int compare(FactoryEntry lhs, FactoryEntry rhs) { + return lhs.name.compareToIgnoreCase(rhs.name); + } + }); + + // reset shall be last + register(R.string.caches_filter_clear, null); + } + + private void register(int resourceId, Class<? extends IFilterFactory> factoryClass) { + registry.add(new FactoryEntry(res.getString(resourceId), factoryClass)); } public void selectFilter(final RunnableWithArgument<IFilter> runAfterwards) { final AlertDialog.Builder builder = new AlertDialog.Builder((Activity) activity); builder.setTitle(R.string.caches_filter); - ArrayList<String> names = new ArrayList<String>(registry.size() + 1); - for (Entry<Integer, Class<? extends IFilterFactory>> entry : registry.entrySet()) { - names.add(res.getString(entry.getKey())); - } - Collections.sort(names); - names.add(res.getString(R.string.caches_filter_clear)); - final String[] array = names.toArray(new String[names.size()]); - builder.setItems(array, new DialogInterface.OnClickListener() { + final ArrayAdapter<FactoryEntry> adapter = new ArrayAdapter<FactoryEntry>((Activity) activity, android.R.layout.select_dialog_item, registry); + + builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int itemIndex) { - // no filter selected? - if (itemIndex >= registry.size()) { + FactoryEntry entry = adapter.getItem(itemIndex); + // reset? + if (entry.filterFactory == null) { runAfterwards.run(null); } else { - final String name = array[itemIndex]; - for (Entry<Integer, Class<? extends IFilterFactory>> entry : registry.entrySet()) { - if (name.equals(res.getString(entry.getKey()))) { - Class<? extends IFilterFactory> producer = entry.getValue(); - try { - IFilterFactory factory = producer.newInstance(); - selectFromFactory(factory, name, runAfterwards); - } catch (Exception e) { - Log.e("selectFilter", e); - } - return; - } + try { + IFilterFactory factoryInstance = entry.filterFactory.newInstance(); + selectFromFactory(factoryInstance, entry.name, runAfterwards); + } catch (Exception e) { + Log.e("selectFilter", e); } } } @@ -89,11 +109,8 @@ public final class FilterUserInterface { final AlertDialog.Builder builder = new AlertDialog.Builder((Activity) activity); builder.setTitle(menuTitle); - final String[] names = new String[filters.length]; - for (int i = 0; i < filters.length; i++) { - names[i] = filters[i].getName(); - } - builder.setItems(names, new DialogInterface.OnClickListener() { + final ArrayAdapter<IFilter> adapter = new ArrayAdapter<IFilter>((Activity) activity, android.R.layout.select_dialog_item, filters); + builder.setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { runAfterwards.run(filters[item]); } |