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

  • Home
  • SEARCH
  • 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 9145325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:29:09+00:00 2026-06-17T10:29:09+00:00

I’m trying to use a ListView as the main navigation feature in a magazine-reader

  • 0

I’m trying to use a ListView as the main navigation feature in a magazine-reader app. I want each View in the list to display the headline of a story below an image associated with it. I have created an XML layout with a vertically-oriented LinearLayout with an ImageView on top and TextView on the bottom, and am trying to create a custom adapter that inflates this layout in the getView method and puts the image/text resources into it. Here is my code:

package com.example.caliber;
import java.util.List;
import com.example.caliber.R;

public class MainActivity extends Activity {
Spinner issues, sections;
Button blog;
ListView mainGal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] issueList = {"issues", "fall '12", "spring '12", "fall '11", "spring '12", 
                            "fall '10", "spring '10"};
    String[] sectionList = {"sections", "campus", "entertainment", "lifestyle", "love&sex", "tech&web", "Current" };

    ArrayAdapter<String> aaIssues = new ArrayAdapter<String>(this, R.layout.spin_item, issueList);
    ArrayAdapter<String> aaSections = new ArrayAdapter<String>(this,   
R.layout.spin_item, sectionList);

    blog = (Button)findViewById(R.id.blogBtn);

    issues = (Spinner)findViewById(R.id.issueSpin);
    sections = (Spinner)findViewById(R.id.sectionSpin);             
    issues.setAdapter(aaIssues);
    sections.setAdapter(aaSections);


    OnClickListener mbListener = new OnClickListener() {
        @Override
            public void onClick(View v){
        issues.setAlpha((float) 0.5);
        sections.setAlpha((float) 0.5);
        blog.setAlpha((float) 0.5);
        }
     };


    ImageButton mainbtn = (ImageButton)findViewById(R.id.cbutton);      
    mainbtn.setOnClickListener(mbListener);

    mainGal = (ListView)findViewById(R.id.mainGal);
    GalAdapter ga = new GalAdapter();
    mainGal.setAdapter(ga);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class GalAdapter extends ArrayAdapter<String> {
    Activity context;
    String[] headlines = {"Alpha bravo charlie delta", "Echo foxtrot golf 
hotel", "indigo juliette kilo lima"};
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};

    public GalAdapter() {
        super (MainActivity.this, R.layout.galleryview);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater li = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
        View story = li.inflate(R.layout.galleryview, parent);
        ImageView image = (ImageView)story.findViewById(R.id.image);
        TextView headline = (TextView)story.findViewById(R.id.headline);

        image.setImageResource(images[position]);
        headline.setText(headlines[position]);
        return story;       
}   

The app launches fine and I can see the listview’s background, but there are no items on the list. Why is this? As is probably clear, I’m very new to android development, but have a good understanding of the fundamentals of Java. I’m aware this is due to a fundamental misunderstanding I have here, perhaps an error in one of the constructors (I’m still a little fuzzy on what some of the parameters are). Anyway, I’ll be very appreciative if anyone can help me understand these issues more deeply.

Thanks in advance

p.s. is it poor form to categorize this under both android and java? should I just use android in the future?

—EDIT:

    class GalAdapter extends ArrayAdapter<String> {
    Activity context;
    String[] hlines;
    int[] imgs;

    public GalAdapter(String[] hlines, int[] imgs) {
        super (MainActivity.this, R.layout.galleryview , hlines);
        this.hlines = hlines;
        this.imgs = imgs;
    }       
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if (convertView == null){
        LayoutInflater li = (LayoutInflater)context. getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.galleryview, parent);
        ImageView image = (ImageView)convertView.findViewById(R.id.image);
        TextView headline = (TextView)convertView.findViewById(R.id.headline);

        image.setImageResource(imgs[position]);
        headline.setText(hlines[position]);}
        return convertView;                                 
    }
    }

Now I’m getting a null pointer exception on the line where I call the LayoutInflator. What pointer is it referring to? I’m assuming I’m not passing the arrays (String[] hlines + int[] imgs) into it properly… Here is where I make the call:

    String[] headlines ={ "Echo foxtrot golf hotel", "Echo foxtrot golf hotel", "indigo   
juliette kilo lima"};
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
            ....
    mainGal = (ListView)findViewById(R.id.mainGal);
    GalAdapter ga = new GalAdapter(headlines, images);
    mainGal.setAdapter(ga);
  • 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-17T10:29:11+00:00Added an answer on June 17, 2026 at 10:29 am

    Your adapter is not created properly. In order to use ArrayAdapter, you must either:

    1. Supply the list of items to the super implementation
    2. Add the items directly to the adapter, or
    3. Override all of the methods necessary to categorize the data the adapter holds, such as getCount(), getItem(), etc.

    The simplest fix is to change your constructor like so:

    public GalAdapter() {
        super(MainActivity.this, R.layout.galleryview, headlines);
    }
    

    What this does is allows the adapter implementation to use one of your arrays to determine how many items are in the list, and what data it should return when getItem() is called. Since you are overriding getView(), the second parameter isn’t even really necessary and you could probably get away with super(MainActivity.this, 0, headlines) as well.

    If your final implementation it to gather data from multiple arrays like this, you may ultimately be better of creating your custom adapter as a subclass of BaseAdapter directly, as you are already bypassing most of the functionality ArrayAdapter provides as convenience.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
i want to parse a xhtml file and display in UITableView. what is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.

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.