Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

New Forums: Nexus Player | Nexus 9
Like Tree1Likes
  • 1 Post By jonbonazza

test: Reply
LinkBack Thread Tools
Old December 30th, 2012, 03:44 PM   #1 (permalink)
New Member
Thread Author (OP)
Join Date: Dec 2012
Posts: 6
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Click on listview with icon: new activity

Hi everybody, I'm a beginner and I need your help for my app .

I would like to make a listview with items and icons, I found and made some code for it (I post only the interesting part, no import, etc.):

My main Layout

[HIGH]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical" >

android:layout_height="wrap_content" />


The layout for one item of my list

[HIGH]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" >

android:layout_height="wrap_content" />


The values:

<string-array name="noms">
<item>Nouveau Mexique</item>

<array name="icones">

My main class:

public class sg_menu extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

Context ctx = getApplicationContext();
Resources res = ctx.getResources();

String[] options = res.getStringArray(R.array.noms);
TypedArray icons = res.obtainTypedArray(R.array.icones);

setListAdapter(new ImageAndTextAdapter(ctx, R.layout.sg_list,
options, icons));

And the adapter class

[HIGH]public class ImageAndTextAdapter extends ArrayAdapter<String> {

private LayoutInflater mInflater;

private String[] mStrings;
private TypedArray mIcons;

private int mViewResourceId;

public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);

mInflater = (LayoutInflater)ctx.getSystemService(

mStrings = strings;
mIcons = icons;

mViewResourceId = viewResourceId;

public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);

ImageView iv = (ImageView)convertView.findViewById(R.id.option_ic on);

TextView tv = (TextView)convertView.findViewById(R.id.option_tex t);

return convertView;

How can I change to another activity when I click on list item. In a best way, I would like to go on activity automaticaly depending on the item I clicked. In 2 words:

Click on item "Arizona" -> go to activity named "arizona.xml"

I don't want to use manualy the "if" for each item (because of quantity!)

Sorry for my bad English and thank you very much !

EDIT: oops, I think I'm on the wrong section, please can you replace my topic on the dev section?

dusty_35 is offline  
Last edited by dusty_35; December 30th, 2012 at 06:40 PM.
Reply With Quote
sponsored links
Old January 3rd, 2013, 10:59 AM   #2 (permalink)
New Member
Thread Author (OP)
Join Date: Dec 2012
Posts: 6
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts

dusty_35 is offline  
Reply With Quote
Old January 4th, 2013, 08:52 AM   #3 (permalink)
Senior Member
jonbonazza's Avatar
Join Date: Jul 2010
Gender: Male
Posts: 1,935
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts

The best you can do with how you have it set up is to override the onListItemClick() method in your ListActivity subclass and have a switch statement on the position, and depending on the position, start a new activity, passing the string that was clicked along with the intent. Then, in your new Activity, have a series of conditionals that analyze the string passed in with the intent and set the content view accordingly.

It doesn't scale well by any means, but without making some drastic changes to your design, it's the best you can do.
dusty_35 likes this.
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Old January 4th, 2013, 02:54 PM   #4 (permalink)
New Member
Thread Author (OP)
Join Date: Dec 2012
Posts: 6
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts

Thank you jonbonazza for your answer!

It's looks very hard for me, I understood what you said but I think there's too much things I have to do for my simple application... I can't find any simple example of what I want but I think that one application must have this kind of code. Maybe I will stay with the "old" method with textes in strings and simple vertical layout for my fake listview (icon+textview). That's a newbie method which works fine but not professional at all...

Any other idea between my method and yours?
dusty_35 is offline  
Reply With Quote

Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

All times are GMT -5. The time now is 04:11 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.