views:

83

answers:

1

Hey there,

I'm looking to be able to open up a new view or activity when I click on an item in my ListView.

Currently I have a list of restaurants, and when i click on a particular restaurant I want it to open up another screen that will show its address, google map etc.

What I need help with is knowing how to set click events on the items in the list. At the moment I dont have a database of the items, they're just Strings. Can someone help me with getting me to this stage?

Thanks alot.

package com.example.androidrestaurant;

import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.TextView; import android.app.ListActivity;

public class Dundrum extends ListActivity { TextView selection;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, DUNDRUM));
    getListView().setTextFilterEnabled(true);

  }

static final String[] DUNDRUM = new String[] {

    "Ananda",
    "Brambles Cafe", "Brannigans", "Buona Sera",
    "Cafe Mao", "Cafe Mimo",
    "Dante", "Douglas & Kaldi Terrace Cafe",
    "Eddie Rockets",
    "Frango's World Cuisine",
    "Nando's",
    "Overends Restaurant @ Airfield House",
    "Pizza Hut",
    "Roly Saul",
    "Siam Thai","Smokey Joes","Sohag Tandoori",
    "TGI Friday","The Rockfield Lounge", "Winters Bar" };
};
+1  A: 

If you are using the ListView in a ListActivity, override onListItemClick(). Otherwise, use setOnItemClickListener() with the ListView. In either case, you are given a position that is the index into your array.

See here for an sample project.

CommonsWare
I edited my code into the above post.Do i need to add in a onListItemClick() in here to open up new activities?
Capsud
You need to add an `onListItemClick()` to find out when list items get clicked.
CommonsWare
Ok and i'm wondering about activities and views next.If I have like 50 restaurants, surely I dont have to have 50 different activities/xml files in my project for each seperate one? What would be the best way of doing this?
Capsud