简体   繁体   中英

How to display images in simple listview listadapter in android

I want to display image in simple listview, we have arraylist HashMap which have values like city, description, etc. & also the base64 image string which we have to convert to image and set to the each row of of the ListView and List row xml is listview_row.xml

This is my code for displaying the text fields in listview, which is done:

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

My question is how to convert and bind image to adapter to display it on listview's each row and also get's error on converting image --- bad base64

I think This Tutorial is exactly what you're looking for.

Edit:

Try this:

Android Encode-Decode

For converting Base64 string, try this-

Assuming your image data is a String called myImageData:

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

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