• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Click on listview with icon: new activity

dusty_35

Lurker
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"
android:orientation="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_icon);
iv.setImageDrawable(mIcons.getDrawable(position));

TextView tv = (TextView)convertView.findViewById(R.id.option_text);
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 :o and thank you very much ! :D


EDIT: oops, I think I'm on the wrong section, please can you replace my topic on the dev section? :o
 
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.
 
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? :rolleyes:
 
Back
Top Bottom