Re: [android-beginners] Images Reused in a ListView
Can you paste some code On Tue, Apr 20, 2010 at 5:19 PM, Tom F M White fred...@gmail.com wrote: I've made a custom ListAdaptor, with associated View, that displays an icon on the left of each list item, with three lines of text to the right. In MyView.onCreate(), the icons are populated using imageView.setImageDrawable(Drawable.createFromStream(URL)); each item on the list is it's own object, so each of these is done individually for each list item. However when viewing the list, the first few items display correctly, but as you scroll down the list, those images that were not visible initially are replaced with the same images used in the top few items, so 4/5 images end up reused for the entire list. The text for each list item is correct. What is going wrong? Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -- When I raise my flashing sword, and my hand takes hold on judgment, I will take vengeance upon mine enemies, and I will repay those who haze me. Oh, Lord, raise me to Thy right hand and count me among Thy saints. -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Images Reused in a ListView
TripleText is just an object for storing 4 strings, 3 for the text, 1 for the image URL. Rest of the code follows, sorry it's quite long... TripleTextView: public class TripleTextView extends LinearLayout { private TextView mText1; private TextView mText2; private TextView mText3; private String imageURL; private ImageView image; private Drawable imageDrawable; public TripleTextView(Context context, TripleText tt) { super(context); this.setOrientation(HORIZONTAL); //add image imageURL = tt.getImageURL(); try { Log.v(TTV,Loading Drawable from: +imageURL); imageDrawable = Drawable.createFromStream(new URL(imageURL).openStream(), src); Log.v(TTV,Image created ok); } catch (MalformedURLException e) { Log.v(TTV,Malformed URL); } catch (IOException e) { Log.v(TTV,IO Exception); } image = new ImageView(context); image.setImageDrawable(imageDrawable); addView(image, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout subLayout = new LinearLayout(context); subLayout.setOrientation(VERTICAL); mText1 = new TextView(context); mText1.setText(tt.getText1()); subLayout.addView(mText1, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText2 = new TextView(context); mText2.setText(tt.getText2()); subLayout.addView(mText2, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText3 = new TextView(context); mText3.setText(tt.getText3()); subLayout.addView(mText3, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(subLayout, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText1(String words) { mText1.setText(words); } public void setText2(String words) { mText2.setText(words); } public void setText3(String words) { mText3.setText(words); } public void setImageURL(String imageURL) { this.imageURL = imageURL; } } TripleListAdapter: public class TripleListAdapter extends BaseAdapter { private Context mContext; private ListTripleText mItems = new ArrayListTripleText(); public TripleListAdapter(Context context) { mContext = context; } public void addItem(TripleText it) { mItems.add(it); } public void setListItems(ListTripleText lit) { mItems = lit; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public boolean areAllItemsSelectable() { return false; } public boolean isSelectable(int position) { try{ return mItems.get(position).isSelectable(); }catch (IndexOutOfBoundsException aioobe){ return false; } } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TripleTextView ttv; if (convertView == null) { ttv = new TripleTextView(mContext, mItems.get(position)); } else { ttv = (TripleTextView) convertView; ttv.setText1(mItems.get(position).getText1()); ttv.setText2(mItems.get(position).getText2()); ttv.setText3(mItems.get(position).getText3()); ttv.setImageURL(mItems.get(position).getImageURL()); } return ttv; } } On 20/04/2010 15:25, Martin Obreshkov wrote: Can you paste some code On Tue, Apr 20, 2010 at 5:19 PM, Tom F M White fred...@gmail.com mailto:fred...@gmail.com wrote: I've made a custom ListAdaptor, with associated View, that displays an icon on the left of each list item, with three lines of text to the right. In MyView.onCreate(), the icons are populated using imageView.setImageDrawable(Drawable.createFromStream(URL)); each item on the list is it's own object, so each of these is done individually for each list item. However when viewing the list, the first few items display correctly, but as you scroll down the list, those images that were not visible initially are replaced with the same images used in the top few items, so 4/5 images end up reused for the entire list. The text for each list item is correct. What is going wrong? Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To
Re: [android-beginners] Images Reused in a ListView
Tom F M White wrote: TripleText is just an object for storing 4 strings, 3 for the text, 1 for the image URL. Rest of the code follows, sorry it's quite long... TripleTextView: public class TripleTextView extends LinearLayout { private TextView mText1; private TextView mText2; private TextView mText3; private String imageURL; private ImageView image; private Drawable imageDrawable; public TripleTextView(Context context, TripleText tt) { super(context); this.setOrientation(HORIZONTAL); //add image imageURL = tt.getImageURL(); try { Log.v(TTV,Loading Drawable from: +imageURL); imageDrawable = Drawable.createFromStream(new URL(imageURL).openStream(), src); Log.v(TTV,Image created ok); } catch (MalformedURLException e) { Log.v(TTV,Malformed URL); } catch (IOException e) { Log.v(TTV,IO Exception); } image = new ImageView(context); image.setImageDrawable(imageDrawable); addView(image, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout subLayout = new LinearLayout(context); subLayout.setOrientation(VERTICAL); mText1 = new TextView(context); mText1.setText(tt.getText1()); subLayout.addView(mText1, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText2 = new TextView(context); mText2.setText(tt.getText2()); subLayout.addView(mText2, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText3 = new TextView(context); mText3.setText(tt.getText3()); subLayout.addView(mText3, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(subLayout, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText1(String words) { mText1.setText(words); } public void setText2(String words) { mText2.setText(words); } public void setText3(String words) { mText3.setText(words); } public void setImageURL(String imageURL) { this.imageURL = imageURL; } } TripleListAdapter: public class TripleListAdapter extends BaseAdapter { private Context mContext; private ListTripleText mItems = new ArrayListTripleText(); public TripleListAdapter(Context context) { mContext = context; } public void addItem(TripleText it) { mItems.add(it); } public void setListItems(ListTripleText lit) { mItems = lit; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public boolean areAllItemsSelectable() { return false; } public boolean isSelectable(int position) { try{ return mItems.get(position).isSelectable(); }catch (IndexOutOfBoundsException aioobe){ return false; } } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TripleTextView ttv; if (convertView == null) { ttv = new TripleTextView(mContext, mItems.get(position)); } else { ttv = (TripleTextView) convertView; ttv.setText1(mItems.get(position).getText1()); ttv.setText2(mItems.get(position).getText2()); ttv.setText3(mItems.get(position).getText3()); ttv.setImageURL(mItems.get(position).getImageURL()); } return ttv; } } In getView(), in the case where convertView is not null, you are calling setImageURL(), then doing nothing with that value to actually load in the replacement image. Also, you are downloading the images on the main application thread. That's going to be a problem -- your UI will freeze, and eventually Android may kill it off with an application not responding error. Please download your images off the main application thread, such as via an AsyncTask. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 2.0 Available! -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Images Reused in a ListView
Thanks for your help. On 20/04/2010 15:37, Mark Murphy wrote: Tom F M White wrote: TripleText is just an object for storing 4 strings, 3 for the text, 1 for the image URL. Rest of the code follows, sorry it's quite long... TripleTextView: public class TripleTextView extends LinearLayout { private TextView mText1; private TextView mText2; private TextView mText3; private String imageURL; private ImageView image; private Drawable imageDrawable; public TripleTextView(Context context, TripleText tt) { super(context); this.setOrientation(HORIZONTAL); //add image imageURL = tt.getImageURL(); try { Log.v(TTV,Loading Drawable from: +imageURL); imageDrawable = Drawable.createFromStream(new URL(imageURL).openStream(), src); Log.v(TTV,Image created ok); } catch (MalformedURLException e) { Log.v(TTV,Malformed URL); } catch (IOException e) { Log.v(TTV,IO Exception); } image = new ImageView(context); image.setImageDrawable(imageDrawable); addView(image, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout subLayout = new LinearLayout(context); subLayout.setOrientation(VERTICAL); mText1 = new TextView(context); mText1.setText(tt.getText1()); subLayout.addView(mText1, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText2 = new TextView(context); mText2.setText(tt.getText2()); subLayout.addView(mText2, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText3 = new TextView(context); mText3.setText(tt.getText3()); subLayout.addView(mText3, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(subLayout, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText1(String words) { mText1.setText(words); } public void setText2(String words) { mText2.setText(words); } public void setText3(String words) { mText3.setText(words); } public void setImageURL(String imageURL) { this.imageURL = imageURL; } } TripleListAdapter: public class TripleListAdapter extends BaseAdapter { private Context mContext; private ListTripleText mItems = new ArrayListTripleText(); public TripleListAdapter(Context context) { mContext = context; } public void addItem(TripleText it) { mItems.add(it); } public void setListItems(ListTripleText lit) { mItems = lit; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public boolean areAllItemsSelectable() { return false; } public boolean isSelectable(int position) { try{ return mItems.get(position).isSelectable(); }catch (IndexOutOfBoundsException aioobe){ return false; } } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TripleTextView ttv; if (convertView == null) { ttv = new TripleTextView(mContext, mItems.get(position)); } else { ttv = (TripleTextView) convertView; ttv.setText1(mItems.get(position).getText1()); ttv.setText2(mItems.get(position).getText2()); ttv.setText3(mItems.get(position).getText3()); ttv.setImageURL(mItems.get(position).getImageURL()); } return ttv; } } In getView(), in the case where convertView is not null, you are calling setImageURL(), then doing nothing with that value to actually load in the replacement image. Also, you are downloading the images on the main application thread. That's going to be a problem -- your UI will freeze, and eventually Android may kill it off with an application not responding error. Please download your images off the main application thread, such as via an AsyncTask. -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en