Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid passing image to a ListView containing an ImageView
    primarykey
    data
    text
    <p>I'm creating a ListView that contains two TextViews and an ImageView.</p> <p>I'm passing data to my SimpleAdapter using a Hashmap, so I convert the ids of the images to a string and then later in the SimpleAdapter I convert them back to integers to set the image resource.</p> <p>However that doesn't seem to work.</p> <p>The relevant code is:</p> <p>The oncreate of my activity</p> <pre><code>clubImages = new Integer[] { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5 }; ListView lv = (ListView)findViewById(android.R.id.list); // create the grid item mapping String[] from = new String[] {"image"}; int[] to = new int[] {R.id.club_image}; // prepare the list of all records List&lt;HashMap&lt;String, String&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); for(int i = 0; i &lt; eventTitles.length; i++){ HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put("image", getString(clubImages[i])); fillMaps.add(map); } // fill in the grid_item layout SimpleAdapter adapter = new MySimpleAdapter(this, fillMaps, R.layout.eventview_row, from, to); lv.setAdapter(adapter); </code></pre> <p>the simpleadapter class</p> <pre><code>public class MySimpleAdapter extends SimpleAdapter { private List&lt;HashMap&lt;String, String&gt;&gt; results; public MySimpleAdapter(Context context, List&lt;HashMap&lt;String, String&gt;&gt; data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.results = data; } public View getView(int position, View view, ViewGroup parent){ Typeface type = Typeface.createFromAsset(getAssets(), "fonts/aircruiser.ttf"); View v = view; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.eventview_row, null); } mClubImageImageView = (ImageView) v.findViewById(R.id.club_image); int ClubImageHelper = Integer.parseInt(results.get(position).get("image").toString()); mClubImageImageView.setImageResource(ClubImageHelper); return v; } } </code></pre> <p>The error in my logcat</p> <pre><code>E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload