2015-02-17 26 views
0

我有一個Android應用程序與Google Cloud Endpoints一起工作。我的一項活動向數據存儲發送請求以獲取Message實體的列表。 該實體有一個byte[]字段,其​​中包含縮略圖圖像。還有兩個其他字段:作者和時間戳。如何使用ImageView和SimpleAdapter?

然後,該活動必須顯示一個ListView,同時顯示縮略圖和作者以及時間戳記字段。這裏是我當前的代碼,只顯示我的一些資源,圖像和作者/時間戳字段:

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" /> 

而現在的部分,我管理SimpleAdapter用哈希映射列表:

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); 

回答

1

您應該使用BitmapFactory解碼字節數組位圖,並將其存儲到與新的密鑰(如「作者」地圖,「T IMESTAMP「)

例如:

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 }); 

和使用粘結劑:

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; 
        } 
       }); 
+0

它完美,謝謝。 – Kritias 2015-02-18 15:18:22

相關問題