Solved

Android inflator / adapter issue

Posted on 2013-01-17
2
690 Views
Last Modified: 2013-07-23
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:

The MainActivity:
package com.example.caliber;

import java.util.List;

import com.example.caliber.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

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

		
		


			
	}
		
		}	
		}

Open in new window


and the XML file for the layout I want inflated (although I'm 99% sure the issue is in the Activity itself) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <ImageView
        android:id="@+id/image"
    	android:layout_width="match_parent"
    	android:layout_height="wrap_content"></ImageView>
    
    <TextView
        android:id="@+id/headline"
    	android:layout_width="match_parent"
    	android:layout_height="wrap_content"></TextView>
   
</LinearLayout>

Open in new window


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?
0
Comment
Question by:dm4488
  • 2
2 Comments
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
Comment Utility
I think the problem is in the GalAdapter class, you can probably find the changes that I made and put them to your code.


mainGal = (ListView)findViewById(R.id.mainGal);
//pass the Context to GalAdapter
GalAdapter ga = new GalAdapter(this); 
mainGal.setAdapter(ga);
		
        // New GalAdapter class
	class GalAdapter extends ArrayAdapter<String> {
		List<String> stories;
		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};
		Context context;
		
		public GalAdapter(Context con) {
			super (con, R.layout.galleryview);
			
			// Convert String Array to a List so getCount, getItemID, and getItem can
                        // be overridden
			stories = Arrays.asList(headlines);
			//Set this context to the one that was passed to the Constructor
                       this.context = con;
		}
		public int getCount() {
			return stories.size();
			}

			public String getItem(int position) {
			return stories.get(position);
			}

			public long getItemId(int position) {
			return position;
			}
		
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent){

 // This line of code retrieves the headline (or String) from the List<String> stories
                    String temp = stories.get(position);
			
		     if (convertView == null) {
		    	 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    	 convertView = inflater.inflate(R.layout.galleryview, null);
		     }
		     
			ImageView image = (ImageView)convertView.findViewById(R.id.image);
			TextView headline = (TextView)convertView.findViewById(R.id.headline);
			
			image.setImageResource(images[position]);
			headline.setText(temp);
			return convertView;			
								
		}
		
		
		}

Open in new window

0
 
LVL 12

Expert Comment

by:PCableGuy
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now