Solved

Android inflator / adapter issue

Posted on 2013-01-17
2
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
ID: 38790525
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
ID: 38791064
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What browser will run Java? 7 177
learn programming 8 94
Manage Mobile Phones (devices) 6 136
Bitwise and to sum elements 2 52
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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