Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.