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

OPAL HUTCHINSON
PLUS
OPAL HUTCHINSON
Courses Plus Student 2,611 Points

Trying to open new activity for each item in list from listview

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);


                }
            }


        });

}

1 Answer

OPAL HUTCHINSON
OPAL HUTCHINSON
Courses Plus Student 2,611 Points

I was getting some errors with the syntax of the code, after I got through with that error. The cod would run but not open the other activity. Would give me the "Unfortunately there was an error" and it would just crash.