Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid: why are no elements appearing in my listview?
    primarykey
    data
    text
    <p>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: </p> <pre><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&amp;sex", "tech&amp;web", "Current" }; ArrayAdapter&lt;String&gt; aaIssues = new ArrayAdapter&lt;String&gt;(this, R.layout.spin_item, issueList); ArrayAdapter&lt;String&gt; aaSections = new ArrayAdapter&lt;String&gt;(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&lt;String&gt; { 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; } </code></pre> <p>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. </p> <p>Thanks in advance</p> <p>p.s. is it poor form to categorize this under both android and java? should I just use android in the future?</p> <p>---EDIT:</p> <pre><code> class GalAdapter extends ArrayAdapter&lt;String&gt; { 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; } } </code></pre> <p>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: </p> <pre><code> 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); </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