Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:44:08+00:00 2026-06-01T03:44:08+00:00

I would like to use a SimpleAdapter instead of customizing an ArrayAdapter. Everything is

  • 0

I would like to use a SimpleAdapter instead of customizing an ArrayAdapter. Everything is working except an icon that is associated with each row. The icon relates directly to the label. SO depending on the label, the icon may be different.

Here is sample XML

<profile>
<id>16</id>
<name>Random Name</name>
<site>Random URL</site>
<icon>R.drawable.random_icon</icon>
</profile>

My custom Row layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label" />

<ImageView
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow" />

</LinearLayout>

Now here is where I Parse my XML and set up my Adapter (edited to just relevant parts):

        NodeList children = doc.getElementsByTagName("profile");

    for (int i = 0; i < children.getLength(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();

                    Element e = (Element) children.item(i);

                    map.put("id", ParseXMLMethods.getValue(e, "id"));
        map.put("name", ParseXMLMethods.getValue(e, "name"));
        map.put("site", ParseXMLMethods.getValue(e, "site"));
        map.put("icon", ParseXMLMethods.getValue(e, "icon"));
        mylist.add(map);
    }

           View header = getLayoutInflater().inflate(R.layout.header, null, false);

       ListAdapter adapter = new SimpleAdapter(this, mylist,
            R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon });


    final ListView lv = getListView();
    lv.addHeaderView(header, null, false);
    lv.setSelector(R.color.list_selector);
    setListAdapter(adapter);

The program doesn’t crash. Everything appears as is, I even parse the “site” so when the row is clicked is opens up a web view. I just can’t get the icon to show. Is this even possible with SimpleAdapter?

UPDATE: Here is overriden getView() method:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    int idImage = context.getResources().getIdentifier("icon","drawable", context.getPackageName());

    //   ????????

    return view;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:44:09+00:00Added an answer on June 1, 2026 at 3:44 am

    I can recommend you to achieve this is by using getIdentifier method from resources in an extended SimpleAdapter. But for that, first you need to change your xml a little:

    Instead of saving programmable reference of image into your xml, you should simply save the file name:

    <profile>
      <id>16</id>
      <name>Random Name</name>
      <site>Random URL</site>
      <icon>random_icon</icon>
    </profile>
    

    Now, extend your SimpleAdapter to your own adapter class and override getView method. In that method you can re-construct resource id from the name and set it to your ImageView:

    int idImage = context.getResources().getIdentifier("nameOfResource", "drawable", context.getPackageName());
    

    Update

    public class MyAdapter extends SimpleAdapter {
    
        private Context context;
    
        List<HashMap<String, String>> lstData = new ArrayList<HashMap<String,String>>();
    
        public MyAdapter(Context context, List<HashMap<String, String>> items) {
            super(context, items, android.R.id.text1, R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon });
    
            this.context = context;
            lstData = items;
        }
    
    
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            //smart initialization      
            if(convertView == null){
                LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.rowlayout, parent, false);
    
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.label);
                holder.img = (ImageView) convertView.findViewById(R.id.icon);
                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();
    
            //get item
            HashMap<String, String> map = lstData.get(position);
    
            //setting title
            holder.title.setText(map.get("name"));
    
            int idImage = context.getResources().getIdentifier(map.get("icon"),"drawable", context.getPackageName());
            holder.img.setImageResource(idImage);
    
            return convertView;
        }
    
    
    
        //this is better approach as suggested by Google-IO for ListView
        private static class ViewHolder{
            TextView title;
            ImageView img;
        }
    
    }
    

    *this class code is a reference for you to see how your adapter should look like. The more appropriate way would be to use ArrayAdapter, but since you are already using SimpleAdapter so I’ve just extended its capabilities

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a component that exposes the datasource property, but instead
I would like to use the group_by method, but instead of using a column
I have a list view that is built using a simpleAdapter, each row is
I would like use argparse to parse the arguments that it knows and then
I would like to use a language that I am familiar with - Java,
I have a ListView that uses SimpleAdapter, each row has 2 TextViews, and I
I currently have some large strings that I would like use as test data
I would like to use complex numbers as defined in C99, but I need
I would like to use HTML 4.01 Strict, and used a DOCTYPE of it
I would like to use KVO in the following context: 1) In touchesBegan:withEvent: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.