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
OPAL HUTCHINSON
Courses Plus Student 2,611 PointsHow 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
Steve Hunter
57,712 PointsHi 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.
wesley franks
6,198 PointsI 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.
OPAL HUTCHINSON
Courses Plus Student 2,611 PointsOPAL HUTCHINSON
Courses Plus Student 2,611 PointsGood 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 {