簡體   English   中英

如何在Android中的簡單ListView ListAdapter中顯示圖像

[英]How to display images in simple listview listadapter in android

我想在簡單的listview中顯示圖像,我們有arraylist HashMap ,其值如城市,描述等。還有base64圖像字符串,我們必須將其轉換為image並設置為ListView和List row xml的每一行。是listview_row.xml

這是我的代碼,用於在列表視圖中顯示文本字段,此操作完成了:

ArrayList<HashMap<String, String>> arllist = new ArrayList<HashMap<String, String>>();
Intent intent = getIntent();
arllist = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("map");

int length = arllist.size();
for(int i=0; i<length; i++)
{   
    try{            
        String stringToConvert = arllist.get(i).get("image");
        byte[] decodedString = Base64.decode(stringToConvert, Base64.NO_PADDING);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ImageView image = (ImageView)this.findViewById(R.id.img);
        image.setImageBitmap(decodedByte);
    }
    catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(productlist.this, e.toString(), Toast.LENGTH_LONG).show();
    }
}



SimpleAdapter listadapter = new SimpleAdapter(this, arllist, R.layout.list_item,
      new String[] {"city", "category", "description", "mobile", "landmark"},
      new int[] {R.id.city, R.id.category, R.id.desc, R.id.mobile, R.id.landmark});

ListView list=(ListView)findViewById(R.id.list);
list.setAdapter(listadapter);

我的問題是如何將圖像轉換並綁定到適配器以將其顯示在listview的每一行上,並且在轉換圖像時也會出現錯誤---不好的base64

我認為本教程正是您想要的。

編輯:

嘗試這個:

Android編碼解碼

要轉換Base64字符串,請嘗試以下操作-

假設您的圖像數據是一個名為myImageData的字符串:

 byte[] imageAsBytes = Base64.decode(myImageData.getBytes());
    ImageView image = (ImageView)this.findViewById(R.id.ImageView);
    image.setImageBitmap(
            BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM