Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How Can I open a new activity when an item is clicked on in the listview?

Good Day Guys, I need some assistance on the following

Android ...................................................................................................................................

public class Cities extends Activity {

    ListView Cities ListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

        //get list view from xml
        citiesListView = (ListView)findViewById(R.id.citiesListView); 

         String[] Cities = {
                 "New York",
                 "Atlanta",
                 "Miami",
                 "Toronto"};

        ListView citiesListView = (ListView) findViewById(R.id.citiesListView); 
        citiesListView.setAdapter(cityAdapter);

        cityListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String cities = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(cities.this, cities, Toast.LENGTH_LONG).show();
                    }
                }
        );

.................................................................................................................. How can i have the user bring up a new activity for each item clicked. For example user clicks on New York ..I would like to bring up another list of items. User clicks on Toronto a fresh list of items comes up.

2 Answers

Hi Opal,

To pass between different activities, you can use an Intent. Have a look through the developer docs here for more info on those.

Steve.

Good Day Steve, below is my code. Don't seem to run. and I was also wondering which is best to use the "If" or "case" statements in that case.

........................................................................................................................................................................................................................................................ import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android. view.View; import android. widget. AdapterView; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast;

public class Cities extends Activity {

ListView cityListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cities);

    //get list view from xml
    cityListView = (ListView) findViewById(R.id.cityListView);


    String[] Cities = {
            "New York",
            "Miami",
            "Atlanta",
            "Orlando",
            "Calgary",
            "Toronto",
            "Dallas",
            "Austin",
            "San Diego",
            "Las Vegas"};
    ListAdapter cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Cities);
    ListView cityListView = (ListView) findViewById(R.id.citiesListView);
    cityListView.setAdapter(cityAdapter);

    cityListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String cities = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Cities.this, cities, Toast.LENGTH_LONG).show();

                    if (position == 1) {
                        //code specific to first list item
                        Intent myIntent = new Intent(view.getContext(), NewYork.class);
                        startActivityForResult(myIntent, 0);


                    }
                }


            });


}

I agree with Steve, Intents are the best way to move from one activity to another. You can even send data through the intent to the other activity. It's pretty cool if you ask me. If you need more understanding on intents check out the docs Steve provided above, or YouTube, How to use an intent to move to another activity in android, this action itself works both on buttons, and list view items, essentially their similar. They both act as a "button" per say.

Hopefully I was of some help!

Wesley.