简体   繁体   中英

How to use ImageView with SimpleAdapter?

I have an Android app working with Google Cloud Endpoints. One of my activities sends a request to the datastore to get a list of Message entities. This entity has a byte[] field which contains a thumbnail image. There are two other fields: Author and Timestamp.

Then, the activity must display a ListView showing both the thumbnail and the author and timestamp fields. Here is my current code, which only displays some of my resources image and the author/timestamp fields:

messages_list_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/messages_thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="7dp"
    android:layout_marginTop="7dp"
    android:background="@null"
    android:src="@drawable/photo" />

<TextView
    android:id="@+id/messages_author"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/messages_thumbnail"
    android:layout_marginLeft="7dp"
    android:layout_toRightOf="@id/messages_thumbnail"
    android:textColor="#303030" />

<TextView
    android:id="@+id/messages_timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/messages_thumbnail"
    android:layout_alignParentRight="true"
    android:layout_marginRight="7dp"
    android:textColor="#303030" />

And now the part where I manage the SimpleAdapter with a List of Hashmap:

final List<HashMap<String, Object>> messages = new ArrayList<HashMap<String, Object>>();
            List<Message> listmessages = messagesCollection.getItems();
            for (Message message : listmessages) {
                HashMap<String, Object> element;
                element = new HashMap<String, Object>();
                byte[] miniature = message.decodeMiniature(); //what to do with this?
                element.put("AUTHOR", message.getAuthor());
                element.put("TIMESTAMP", message.getTimestamp());
                messages.add(element);
            }
            ListAdapter simpleAdapter = new SimpleAdapter(context,
                    messages, R.layout.messages_list_layout,
                    new String[] { "AUTHOR", "TIMESTAMP"},
                    new int[] { R.id.messages_author, R.id.messages_timestamp });
            listView_messages.setAdapter(simpleAdapter);

You should use BitmapFactory to decode byte array to bitmap and store it to map with new key (like "AUTHOR", "TIMESTAMP")

for example:

byte[] miniature = message.decodeMiniature();
Bitmap image = BitmapFactory.decodeByteArray(miniature, 0, miniature.length);
element.put("IMAGE", image);
...
ListAdapter simpleAdapter = new SimpleAdapter(context,
                messages, R.layout.messages_list_layout,
                new String[] { "AUTHOR", "TIMESTAMP", "IMAGE"},
                new int[] { R.id.messages_author, R.id.messages_timestamp, R.id.messages_thumbnail });

and use binder:

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                    @Override
                    public boolean setViewValue(View view, Object data,String textRepresentation) 
                    {
                        if((view instanceof ImageView) & (data instanceof Bitmap)) 
                        {
                            ImageView iv = (ImageView) view;
                            Bitmap bm = (Bitmap) data;
                            iv.setImageBitmap(bm);
                            return true;
                        }
                        return false;
                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM