简体   繁体   中英

How to add images to custom listview using Simple Adapter in android

I'm making a movies app using JSON requests, etc. So I have a listview that shows movies data such as actors, directors, parental guide, IMDB etc. Here is the code for putting these data in listview:

ListAdapter adapter = new SimpleAdapter(
                movies.this, contactList,
                R.layout.list_new, new String[]{"name", "actors",
                "story", "imrating", "genre","guide", "director"}, new int[]{R.id.title,
                R.id.actors_txt, R.id.story,  R.id.rating, R.id.genre,R.id.parental, R.id.director_txt, R.id.year, R.id.imageView8, R.id.imageView10, R.id.backgroundimg});

lv.setAdapter(adapter);

.. and here is the problem that I have IMDB icon, so it comes like that <imdbicon> 6.2(from JSON) but the image itself from the app in imageview in the layout file for the listview.

R.id.imageView8, R.id.imageView10, R.id.backgroundimg

these three I want in the listview but I can't figure how

You can make a subclass of SimpleAdapter and override setImageView to get an image based on your text:

ListAdapter adapter = new SimpleAdapter(
                movies.this, contactList,
                R.layout.list_new, new String[]{"name", "actors",
                "story", "imrating", "genre","guide", "director"}, new int[]{R.id.title,
                R.id.actors_txt, R.id.story,  R.id.rating, R.id.genre,R.id.parental, R.id.director_txt, R.id.year, R.id.imageView8, R.id.imageView10, R.id.backgroundimg}) {

    @Override
    public void setViewImage(ImageView v, String value) {

         // here you put code to take your "value" string
         // and get your image, for example using Picasso
         // or Glide, and putting it in the "v" ImageView.
    }
 };

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