ListView, SimpleAdapter and Bitmaps - bug ?

Put your problem here if it does not fit any of the other categories.

ListView, SimpleAdapter and Bitmaps - bug ?

Postby darolla » Sun Mar 07, 2010 5:26 pm

Hi,

I got my ListView which shows one ImageView and two TextViews in every row.

Now I want to load the images NOT from my ressources (as in many examples) but from the internet.

Somehow this doesnt work with the SimpleAdapter. Is this a bug?

Here comes my code:

---------------------------------------------------------------------------

MEINUNGEN.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/bg">

<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:cacheColorHint="#00000000"
android:divider="@drawable/divider" android:dividerHeight="1px"/>

</LinearLayout>

---------------------------------------------------------------------------

MEINUNGEN_LIST_ROW.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingTop="3dip"
android:paddingBottom="3dip" android:paddingLeft="3dip">

<ImageView android:id="@+id/icon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_vertical"
android:src="@drawable/wolfgangroth" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">

<TextView android:id="@+id/name" android:text="Thomas Öchsner"
android:textSize="10px" android:gravity="center_vertical"
android:paddingLeft="6dip" android:paddingTop="6dip"
android:textColor="#cccccc" android:layout_width="fill_parent" android:layout_height="wrap_content"/>

<TextView android:id="@+id/content" android:text="Dämpfer für von der Leyen"
android:textSize="16px" android:gravity="center_vertical"
android:paddingLeft="6dip" android:paddingTop="6dip"
android:textColor="#eeeeee" android:layout_width="fill_parent" android:layout_height="wrap_content"/>

</LinearLayout>

</LinearLayout>

---------------------------------------------------------------------------

public class MeinungenListActivity extends ListActivity {

private static final String ICON = "icon";
private static final String NAME = "name";
private static final String CONTENT = "content";
private static final String INTENT = "intent";
private static final String[] PARAM = { ICON, NAME, CONTENT };

private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.meinungen);

addItem(list, R.drawable.thomasoechsner, "Thomas Öchsner",
"Dämpfer für von der Leyen", new Intent(this,
MeinungenListActivity.class));

addItem(list, R.drawable.marcbeise, "Marc Beise",
"Die Banker ducken sich schon wieder weg", new Intent(this,
MeinungenListActivity.class));

addItem(list, R.drawable.wolfgangroth, "Wolfgang Roth",
"Röttgen setzt auf Angriff", new Intent(this,
MeinungenListActivity.class));

addItem(list, R.drawable.charlottefrank, "Charlotte Frank",
"Sorge um die Vorsorge", new Intent(this,
MeinungenListActivity.class));

SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.meinungen_list_row, PARAM, new int[] { R.id.icon, R.id.name, R.id.content });
setListAdapter(notes);
}

public void addItem(List<Map<String, Object>> data, int icon, String name, String content, Intent intent) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(ICON, icon);
map.put(NAME, name);
map.put(CONTENT, content);
map.put(INTENT, intent);
data.add(map);
}

---------------------------------------------------------------------------

THIS CODE IS THE PROBLEM (please look at "here_comes_it"):

public void addItem(List<Map<String, Object>> data, Bitmap here_comes_it, String name, String content, Intent intent) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(ICON, here_comes_it);
map.put(NAME, name);
map.put(CONTENT, content);
map.put(INTENT, intent);
data.add(map);
}


try {
URL aURL = new URL("http://www.anddev.org/images/avatars/6748766294aa15a585d1fd.jpg");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap here_comes_it = BitmapFactory.decodeStream(is);
is.close();

addItem(list, here_comes_it, "Daniela Kuhr", "Angst vor der Genknolle", new Intent(this, MeinungenListActivity.class));
} catch (Exception e) {
e.printStackTrace();
}

---------------------------------------------------------------------------

do you have any suggestions to use bitmaps with listview and simpleadapter?

thanks !

greetings,
marco
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Top

Postby darolla » Sun Mar 07, 2010 6:29 pm

I solved it :)

http://www.anddev.org/viewtopic.php?p=37390

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 SimpleAdapter notes = new SimpleAdapter(this, list,
  3.  
  4.                                 R.layout.meinungen_list_row, PARAM, new int[] { R.id.icon,
  5.  
  6.                                                 R.id.name, R.id.content });
  7.  
  8.  
  9.  
  10.                 notes.setViewBinder(new MyViewBinder());
  11.  
  12.  
  13.  
  14.                 setListAdapter(notes);
  15.  
  16.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public class MyViewBinder implements ViewBinder {
  3.  
  4.  
  5.  
  6.                 @Override
  7.  
  8.                 public boolean setViewValue(View view, Object data,
  9.  
  10.                                 String textRepresentation) {
  11.  
  12.                        
  13.  
  14.                         if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
  15.  
  16.                                 ImageView iv = (ImageView) view;
  17.  
  18.                                 Bitmap bm = (Bitmap) data;     
  19.  
  20.                                 iv.setImageBitmap(bm); 
  21.  
  22.                                 return true;
  23.  
  24.                         }
  25.  
  26.  
  27.  
  28.                         return false;
  29.  
  30.                 }
  31.  
  32.         }
  33.  
  34.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Greetings,
Marco
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Re: ListView, SimpleAdapter and Bitmaps - bug ?

Postby ancientgeek » Wed May 11, 2011 10:41 am

Hey Darolla, can I ask you something? I believe that "data" in your ViewBinder is a map item? I have a similar problem to yours just that I don't only have 1 image that I have to get from the web. I'm thinking that I may have to use for-loop to iterate through the entire list of urls that I'm using an then setting the images into the imageviews inside the list. Do you have any idea how? Thanks
ancientgeek
Freshman
Freshman
 
Posts: 5
Joined: Mon Apr 25, 2011 7:33 am

Re: ListView, SimpleAdapter and Bitmaps - bug ?

Postby guotingchaopr » Sat Sep 01, 2012 2:02 pm

Hey,Darolla. very honored to see your share
i'm a chinese beginners and i was also met the problem.
so.thank you very much.
But :
if( (view instanceof ImageView) & (data instanceof Bitmap) )
Why did you don't make use of "&&" ? Guo
guotingchaopr
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Sep 01, 2012 1:44 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 24 guests