diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-06-08 20:10:58 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-06-08 20:10:58 +0200 |
| commit | 75ef80dc91bea395f7c7d089d8ca16b83d93e01f (patch) | |
| tree | 9a6d88658d2a5c5bf026e4a845e8dc48fd30b22d /main | |
| parent | 88eda55ecfd95e562f7af410fa374f8d22eb6c9e (diff) | |
| download | cgeo-75ef80dc91bea395f7c7d089d8ca16b83d93e01f.zip cgeo-75ef80dc91bea395f7c7d089d8ca16b83d93e01f.tar.gz cgeo-75ef80dc91bea395f7c7d089d8ca16b83d93e01f.tar.bz2 | |
refactoring: reduce duplicated code
Diffstat (limited to 'main')
| -rw-r--r-- | main/src/cgeo/geocaching/LogViewHolder.java | 12 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/NavigateAnyPointActivity.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/UsefulAppsActivity.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/AbstractViewHolder.java | 19 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/AddressListAdapter.java | 19 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CacheListAdapter.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/FileSelectionListAdapter.java | 28 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/GPXListAdapter.java | 28 |
8 files changed, 69 insertions, 55 deletions
diff --git a/main/src/cgeo/geocaching/LogViewHolder.java b/main/src/cgeo/geocaching/LogViewHolder.java index 1fb3f55..14148d0 100644 --- a/main/src/cgeo/geocaching/LogViewHolder.java +++ b/main/src/cgeo/geocaching/LogViewHolder.java @@ -1,13 +1,14 @@ package cgeo.geocaching; import butterknife.InjectView; -import butterknife.Views; + +import cgeo.geocaching.ui.AbstractViewHolder; import android.view.View; import android.widget.ImageView; import android.widget.TextView; -public class LogViewHolder { +public class LogViewHolder extends AbstractViewHolder { @InjectView(R.id.added) protected TextView date ; @InjectView(R.id.type) protected TextView type; @InjectView(R.id.author) protected TextView author; @@ -19,14 +20,13 @@ public class LogViewHolder { private int position; public LogViewHolder(View rowView) { - Views.inject(this, rowView); - rowView.setTag(this); + super(rowView); } /** * Read the position of the cursor pointed to by this holder. <br/> * This must be called by the UI thread. - * + * * @return the cursor position */ public int getPosition() { @@ -36,7 +36,7 @@ public class LogViewHolder { /** * Set the position of the cursor pointed to by this holder. <br/> * This must be called by the UI thread. - * + * * @param position * the cursor position */ diff --git a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java index 61ba0e5..cc6853b 100644 --- a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java +++ b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java @@ -8,6 +8,7 @@ import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.geopoint.DistanceParser; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; +import cgeo.geocaching.ui.AbstractViewHolder; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.utils.GeoDirHandler; @@ -62,14 +63,13 @@ public class NavigateAnyPointActivity extends AbstractActivity { private String distanceUnit = ""; - protected static class ViewHolder { + protected static class ViewHolder extends AbstractViewHolder { @InjectView(R.id.simple_way_point_longitude) protected TextView longitude; @InjectView(R.id.simple_way_point_latitude) protected TextView latitude; @InjectView(R.id.date) protected TextView date; public ViewHolder(View rowView) { - Views.inject(this, rowView); - rowView.setTag(this); + super(rowView); } } diff --git a/main/src/cgeo/geocaching/UsefulAppsActivity.java b/main/src/cgeo/geocaching/UsefulAppsActivity.java index d6e0ec8..b74db83 100644 --- a/main/src/cgeo/geocaching/UsefulAppsActivity.java +++ b/main/src/cgeo/geocaching/UsefulAppsActivity.java @@ -4,6 +4,7 @@ import butterknife.InjectView; import butterknife.Views; import cgeo.geocaching.activity.AbstractActivity; +import cgeo.geocaching.ui.AbstractViewHolder; import android.app.Activity; import android.content.Intent; @@ -20,13 +21,13 @@ public class UsefulAppsActivity extends AbstractActivity { @InjectView(R.id.apps_list) protected ListView list; - protected static class ViewHolder { + protected static class ViewHolder extends AbstractViewHolder { @InjectView(R.id.title) protected TextView title; @InjectView(R.id.image) protected ImageView image; @InjectView(R.id.description) protected TextView description; public ViewHolder(View rowView) { - Views.inject(this, rowView); + super(rowView); } } @@ -80,7 +81,6 @@ public class UsefulAppsActivity extends AbstractActivity { ViewHolder holder = (ViewHolder) rowView.getTag(); if (null == holder) { holder = new ViewHolder(rowView); - rowView.setTag(holder); } final HelperApp app = getItem(position); diff --git a/main/src/cgeo/geocaching/ui/AbstractViewHolder.java b/main/src/cgeo/geocaching/ui/AbstractViewHolder.java new file mode 100644 index 0000000..cc5cd4d --- /dev/null +++ b/main/src/cgeo/geocaching/ui/AbstractViewHolder.java @@ -0,0 +1,19 @@ +package cgeo.geocaching.ui; + +import butterknife.Views; + +import android.view.View; + +/** + * Abstract super class for all view holders. It is responsible for the invocation of the view injection code and for + * the tagging of views. + * + */ +public abstract class AbstractViewHolder { + + protected AbstractViewHolder(View view) { + Views.inject(this, view); + view.setTag(this); + } + +} diff --git a/main/src/cgeo/geocaching/ui/AddressListAdapter.java b/main/src/cgeo/geocaching/ui/AddressListAdapter.java index eb8b516..6689bb6 100644 --- a/main/src/cgeo/geocaching/ui/AddressListAdapter.java +++ b/main/src/cgeo/geocaching/ui/AddressListAdapter.java @@ -1,5 +1,7 @@ package cgeo.geocaching.ui; +import butterknife.InjectView; + import cgeo.geocaching.R; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.cgeocaches; @@ -24,9 +26,13 @@ public class AddressListAdapter extends ArrayAdapter<Address> { final private LayoutInflater inflater; final private Geopoint location; - private static final class ViewHolder { - TextView label; - TextView distance; + protected static final class ViewHolder extends AbstractViewHolder { + @InjectView(R.id.label) protected TextView label; + @InjectView(R.id.distance) protected TextView distance; + + public ViewHolder(View view) { + super(view); + } } public AddressListAdapter(final Context context) { @@ -45,12 +51,7 @@ public class AddressListAdapter extends ArrayAdapter<Address> { final ViewHolder holder; if (view == null) { view = inflater.inflate(R.layout.addresses_item, null); - - holder = new ViewHolder(); - holder.label = (TextView) view.findViewById(R.id.label); - holder.distance = (TextView) view.findViewById(R.id.distance); - - view.setTag(holder); + holder = new ViewHolder(view); } else { holder = (ViewHolder) view.getTag(); } diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index c27c387..122f835 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -1,7 +1,6 @@ package cgeo.geocaching.ui; import butterknife.InjectView; -import butterknife.Views; import cgeo.geocaching.CacheDetailActivity; import cgeo.geocaching.Geocache; @@ -94,7 +93,7 @@ public class CacheListAdapter extends ArrayAdapter<Geocache> { * view holder for the cache list adapter * */ - protected static class ViewHolder { + protected static class ViewHolder extends AbstractViewHolder { @InjectView(R.id.checkbox) protected CheckBox checkbox; @InjectView(R.id.log_status_mark) protected ImageView logStatusMark; @InjectView(R.id.text) protected TextView text; @@ -106,8 +105,7 @@ public class CacheListAdapter extends ArrayAdapter<Geocache> { @InjectView(R.id.dirimg) protected ImageView dirImg; public ViewHolder(View view) { - Views.inject(this, view); - view.setTag(this); + super(view); } } diff --git a/main/src/cgeo/geocaching/ui/FileSelectionListAdapter.java b/main/src/cgeo/geocaching/ui/FileSelectionListAdapter.java index ea32178..c325f50 100644 --- a/main/src/cgeo/geocaching/ui/FileSelectionListAdapter.java +++ b/main/src/cgeo/geocaching/ui/FileSelectionListAdapter.java @@ -1,5 +1,7 @@ package cgeo.geocaching.ui; +import butterknife.InjectView; + import cgeo.geocaching.R; import cgeo.geocaching.files.IFileSelectionView; import cgeo.geocaching.utils.Log; @@ -17,21 +19,18 @@ import java.util.List; public class FileSelectionListAdapter extends ArrayAdapter<File> { - private IFileSelectionView parentView; - private LayoutInflater inflater; + private final IFileSelectionView parentView; + private final LayoutInflater inflater; public FileSelectionListAdapter(IFileSelectionView parentIn, List<File> listIn) { super(parentIn.getContext(), 0, listIn); parentView = parentIn; + inflater = ((Activity) getContext()).getLayoutInflater(); } @Override public View getView(final int position, final View rowView, final ViewGroup parent) { - if (inflater == null) { - inflater = ((Activity) getContext()).getLayoutInflater(); - } - if (position > getCount()) { Log.w("FileSelectionListAdapter.getView: Attempt to access missing item #" + position); return null; @@ -44,12 +43,7 @@ public class FileSelectionListAdapter extends ArrayAdapter<File> { ViewHolder holder; if (v == null) { v = inflater.inflate(R.layout.mapfile_item, null); - - holder = new ViewHolder(); - holder.filepath = (TextView) v.findViewById(R.id.mapfilepath); - holder.filename = (TextView) v.findViewById(R.id.mapfilename); - - v.setTag(holder); + holder = new ViewHolder(v); } else { holder = (ViewHolder) v.getTag(); } @@ -85,8 +79,12 @@ public class FileSelectionListAdapter extends ArrayAdapter<File> { } } - private static final class ViewHolder { - TextView filepath; - TextView filename; + protected static final class ViewHolder extends AbstractViewHolder { + @InjectView(R.id.mapfilepath) protected TextView filepath; + @InjectView(R.id.mapfilename) protected TextView filename; + + public ViewHolder(View view) { + super(view); + } } } diff --git a/main/src/cgeo/geocaching/ui/GPXListAdapter.java b/main/src/cgeo/geocaching/ui/GPXListAdapter.java index d2bfe16..7f3c33f 100644 --- a/main/src/cgeo/geocaching/ui/GPXListAdapter.java +++ b/main/src/cgeo/geocaching/ui/GPXListAdapter.java @@ -1,5 +1,7 @@ package cgeo.geocaching.ui; +import butterknife.InjectView; + import cgeo.geocaching.GpxFileListActivity; import cgeo.geocaching.R; import cgeo.geocaching.files.GPXImporter; @@ -18,26 +20,27 @@ import java.io.File; import java.util.List; public class GPXListAdapter extends ArrayAdapter<File> { - private GpxFileListActivity activity = null; - private LayoutInflater inflater = null; + private final GpxFileListActivity activity; + private final LayoutInflater inflater; + + protected static class ViewHolder extends AbstractViewHolder { + @InjectView(R.id.filepath) protected TextView filepath; + @InjectView(R.id.filename) protected TextView filename; - private static class ViewHolder { - TextView filepath; - TextView filename; + public ViewHolder(View view) { + super(view); + } } public GPXListAdapter(GpxFileListActivity parentIn, List<File> listIn) { super(parentIn, 0, listIn); activity = parentIn; + inflater = ((Activity) getContext()).getLayoutInflater(); } @Override public View getView(final int position, final View rowView, final ViewGroup parent) { - if (inflater == null) { - inflater = ((Activity) getContext()).getLayoutInflater(); - } - if (position > getCount()) { Log.w("GPXListAdapter.getView: Attempt to access missing item #" + position); return null; @@ -50,12 +53,7 @@ public class GPXListAdapter extends ArrayAdapter<File> { final ViewHolder holder; if (view == null) { view = inflater.inflate(R.layout.gpx_item, null); - - holder = new ViewHolder(); - holder.filepath = (TextView) view.findViewById(R.id.filepath); - holder.filename = (TextView) view.findViewById(R.id.filename); - - view.setTag(holder); + holder = new ViewHolder(view); } else { holder = (ViewHolder) view.getTag(); } |
