drew moore
drew moore

Reputation: 32680

android: why are no elements appearing in my listview?

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);

Upvotes: 0

Views: 130

Answers (2)

devunwired
devunwired

Reputation: 63303

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.

Upvotes: 0

seaplain
seaplain

Reputation: 771

Have a look here https://github.com/cplain/custom-list it will be able to help you a bit, you need to use convertViewin your getView() and I would advise passing in a list into your adapter not making one inside

Upvotes: 1

Related Questions