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
 
Device(s):
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"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>[/HIGH]

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_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>[/HIGH]

The values:

[HIGH]<resources>
<string-array name="noms">
<item>Arizona</item>
<item>Californie</item>
<item>Colorado</item>
<item>Nevada</item>
<item>Nouveau Mexique</item>
<item>Utah</item>
<item>Wyoming</item>
</string-array>

<array name="icones">
<item>@drawable/flag_arizona</item>
<item>@drawable/flag_californie</item>
<item>@drawable/flag_colorado</item>
<item>@drawable/flag_nevada</item>
<item>@drawable/flag_nouveau_mexique</item>
<item>@drawable/flag_utah</item>
<item>@drawable/flag_wyoming</item>
</array>
</resources>[/HIGH]

My main class:

[HIGH]
public class sg_menu extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sg_menu);

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));
}
}
[/HIGH]

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(
Context.LAYOUT_INFLATER_SERVICE);

mStrings = strings;
mIcons = icons;

mViewResourceId = viewResourceId;
}

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

ImageView iv = (ImageView)convertView.findViewById(R.id.option_ic on);
iv.setImageDrawable(mIcons.getDrawable(position));

TextView tv = (TextView)convertView.findViewById(R.id.option_tex t);
tv.setText(mStrings[position]);

return convertView;
}[/HIGH]

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?

Advertisements
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
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nobody?
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
Default

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
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply


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.