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 9035771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:50:09+00:00 2026-06-16T08:50:09+00:00

Am trying to create a custom listview but it cant dispaly am really a

  • 0

Am trying to create a custom listview but it cant dispaly am really a newbie and need the help …. Here’s the code

public class Main_Activity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

setListAdapter((ListAdapter) new MyAdapter(this, 
        android.R.layout.simple_list_item_1,R.id.textView1,
        getResources().getStringArray(R.array.categories)));

}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.list_item, parent, false);
        String[] items = getResources().getStringArray(R.array.categories);

        ImageView image = (ImageView)row.findViewById(R.id.textView1);
        TextView text =(TextView)row.findViewById(R.id.textView1);

        text.setText(items[position]);

        if(items[position].equals("Life")){
            image.setImageResource(R.drawable.lifeico);
        }

        else if(items[position].equals("Corporate")){
            image.setImageResource(R.drawable.corpico);
        }

        else if(items[position].equals("umash")){
            image.setImageResource(R.drawable.umashico);
        }


        return row;
    }
}

//then the listview layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

//using the compound drawable layout

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/lifeico"
android:drawablePadding="5dp"
android:text="@string/textview"
android:textSize="25sp" >

</TextView> 

//and the resource list of items

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="categories">
    <item name="Life">Individual Life</item>
    <item name="Corporate">Corporate Insurance</item>
    <item name="Umash">Umash Funeral Services</item>
</string-array>

</resources>
  • 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-16T08:50:10+00:00Added an answer on June 16, 2026 at 8:50 am

    you really haven’t given us enough to go on (like a logcat of errors if available or a log statement not being reached), it could be any number of things.

    I’d say that there must be issue with the View row xml that you are inflating View row = inflater.inflate(R.layout.list_item,. That could be the incorrect name, but is the entire layout, just the TextView? If so, you should (add the imageView &) put it in a ViewGroup, like RelativeLayout,LinearLayout etc. just like your main layout is. But most alarming is probably:

        ImageView image = (ImageView)row.findViewById(R.id.textView1); <-
        TextView text =(TextView)row.findViewById(R.id.textView1); <-
    

    can you see that you’re referencing two different views by the same id? the latest image command might be screwing it up.

    Moreover, it’s best practices not to do what’s unnecessary in getView since it is called for each row of the ListView. That’s what passing things through the constructor are for (items which you seem to have ignored), so that they’re only done once. Try it like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    
    setListAdapter((ListAdapter) new MyAdapter(
            this, 
            R.layout.list_item,  <--
            R.id.textView1,  <------
            R.id.imageView1, <------ make sure these are all correct
            getResources().getStringArray(R.array.categories)));
    
    }
    
    private class MyAdapter extends ArrayAdapter<String>{
    
        private int resource;
        private int textViewResourceId;
        private int imageViewResourceId;
        private String[] strings;
        private LayoutInflater inflater;
    
        public MyAdapter(Context context, int resource, int textViewResourceId, 
                int imageViewResourceId, String[] strings) {
            super(context, resource, textViewResourceId, strings);
            this.resource = resource;
            this.textViewResourceId = textViewResourceId;
            this.imageViewResourceId  = imageViewResourceId;
            this.strings = strings;
            inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
    
            View row = inflater.inflate(resource, parent, false);
    
            ImageView image = (ImageView)row.findViewById(imageViewResourceId);
            TextView text =(TextView)row.findViewById(textViewResourceId);
    
            text.setText(strings[position]);
    
            if(strings[position].equals("Life")){
                image.setImageResource(R.drawable.lifeico);
            }
    
            else if(strings[position].equals("Corporate")){
                image.setImageResource(R.drawable.corpico);
            }
    
            else if(strings[position].equals("umash")){
                image.setImageResource(R.drawable.umashico);
            }
    
    
            return row;
        }
    

    you could have even passed all your constructor parameters straight through if they were all in the same master class.

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

Sidebar

Related Questions

Hello I am trying to create a custom expandable listview, but the documentation on
I'm trying to create a ListView with custom ArrayAdapter. Following the example from here.
I'm trying to create a custom usercontrol that acts like a button, but i
I'm trying to create a custom ListView layout similar to android.R.layout.simple_list_item_2. However, when I
I am trying to create a custom ListView using an ArrayAdapter . POJO Item
I'm trying to create a listview within a customdialog. listview contains a custom row
I'm trying to create custom view that draws image downloaded from Url. The code
What I've done Hello Guys, I'm trying to create a custom ListView layout. I've
I have a custom ListView class (subclassed from ListView) and I need it to
I'm trying to create custom listview in android. When I try to access my

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.