1. Are you ready for the Galaxy S20? Here is everything we know so far!

Click on listview with icon: new activity

Discussion in 'Android Development' started by dusty_35, Dec 30, 2012.

  1. dusty_35

    dusty_35 Lurker
    Thread Starter

    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:eek:rientation="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_icon);

    TextView tv = (TextView)convertView.findViewById(R.id.option_text);

    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 :eek: 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? :eek:

    1. Download the Forums for Android™ app!


  2. dusty_35

    dusty_35 Lurker
    Thread Starter

  3. jonbonazza

    jonbonazza Android Expert

    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.
  4. dusty_35

    dusty_35 Lurker
    Thread Starter

    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:

Share This Page