From fae5cdad67a34b137664e6d24241e910ab9a2b7d Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Sat, 9 Feb 2013 01:38:54 +0100 Subject: [PATCH] Fixed the issue with images jumping randomly around the list. --- res/layout/event_list_row.xml | 11 +++- res/values/strings.xml | 1 + .../app/network/ImageLoader.java | 57 +++++++++++++------ .../app/ui/lists/EventListActivity.java | 14 +++-- 4 files changed, 60 insertions(+), 23 deletions(-) diff --git a/res/layout/event_list_row.xml b/res/layout/event_list_row.xml index 2e9d90a..3188cdf 100644 --- a/res/layout/event_list_row.xml +++ b/res/layout/event_list_row.xml @@ -3,8 +3,14 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingTop="3px" - android:paddingBottom="3px"> + android:paddingTop="6dp" + android:paddingBottom="6dp"> + + BokCaféen Glassbaren Billettluka + Event-bilde diff --git a/src/com/studentersamfundet/app/network/ImageLoader.java b/src/com/studentersamfundet/app/network/ImageLoader.java index 5bfb055..d6a503b 100644 --- a/src/com/studentersamfundet/app/network/ImageLoader.java +++ b/src/com/studentersamfundet/app/network/ImageLoader.java @@ -15,20 +15,48 @@ import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.TextView; public class ImageLoader extends AsyncTask { + private final static String TAG = ImageLoader.class.getName(); private final static ConcurrentMap cache = new ConcurrentHashMap(); - private final ViewGroup parent; - private final int position; + private final ViewGroup row; private final int imageId; private int progressbarId = -1; - public ImageLoader(ViewGroup parent, int position, int imageViewId) { - this.parent = parent; - this.position = position; + private int hiddenTextViewId = -1; + private String correctText = ""; + + public ImageLoader(ViewGroup row, int imageViewId) { + this.row = row; this.imageId = imageViewId; } + public void setHiddenId(int hiddenTextViewId, String correctText) { + if (hiddenTextViewId < 0 || correctText == null) { + Log.w(TAG, "Invalid parameters to Imageloader.setHiddenId()"); + return; + } + + this.hiddenTextViewId = hiddenTextViewId; + this.correctText = correctText; + } + + private boolean isHiddenIdCorrect(ViewGroup row) { + if (hiddenTextViewId < 0) { + return true; + } + + TextView hiddenIdView = (TextView) row.findViewById(hiddenTextViewId); + CharSequence hiddenId = hiddenIdView.getText(); + + if (hiddenId != null) { + return this.correctText.equals(hiddenId.toString()); + } + + return false; + } + @Override protected Drawable doInBackground(String... params) { if (params.length > 1) @@ -63,18 +91,15 @@ public class ImageLoader extends AsyncTask { protected void onPostExecute(Drawable result) { super.onPostExecute(result); - if (result != null && parent != null) { - ViewGroup row = (ViewGroup)parent.getChildAt(position); - - if (row != null) { - if (progressbarId > 0) { - View pb = row.findViewById(progressbarId); - pb.setVisibility(View.GONE); - } - ImageView image = (ImageView)row.findViewById(imageId); - image.setImageDrawable(result); - image.setVisibility(View.VISIBLE); + if (result != null && row != null && isHiddenIdCorrect(row)) { + if (progressbarId > 0) { + View pb = row.findViewById(progressbarId); + pb.setVisibility(View.GONE); } + + ImageView image = (ImageView)row.findViewById(imageId); + image.setImageDrawable(result); + image.setVisibility(View.VISIBLE); } } diff --git a/src/com/studentersamfundet/app/ui/lists/EventListActivity.java b/src/com/studentersamfundet/app/ui/lists/EventListActivity.java index 05256d6..5c68ca9 100644 --- a/src/com/studentersamfundet/app/ui/lists/EventListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/EventListActivity.java @@ -84,24 +84,28 @@ public class EventListActivity extends BaseListActivity { ListAdapter adapter = new ArrayAdapter(this, R.layout.event_list_row, R.id.event_list_row_text, events) { @Override public View getView(int position, View convertView, ViewGroup parent) { - View row; + ViewGroup row; if (null == convertView) { LayoutInflater inflater = getLayoutInflater(); - row = inflater.inflate(R.layout.event_list_row, null); + row = (ViewGroup)inflater.inflate(R.layout.event_list_row, null); } else { - row = convertView; + row = (ViewGroup)convertView; } final Event e = getItem(position); + + /* Set hidden text: */ + TextView hiddenView = (TextView) row.findViewById(R.id.event_list_row_hidden_text); + hiddenView.setText(Integer.toString(e.id)); /* Set images: */ String thumbnailUri = e.getThumbnail().toString(); - ImageLoader loader = new ImageLoader(parent, position, R.id.event_list_row_image); + ImageLoader loader = new ImageLoader(row, R.id.event_list_row_image); loader.setProgressbar(R.id.event_list_row_image_pb); + loader.setHiddenId(R.id.event_list_row_hidden_text, Integer.toString(e.id)); loader.execute(thumbnailUri); - /* Set text: */ TextView titleView = (TextView) row.findViewById(R.id.event_list_row_text); titleView.setText(Html.fromHtml(e.title));