How To View Detailed Data Of A Specific List View Item
Let's say i have a list of cities from a country. If the user clicks one of them, i want a new screen to be displayed, with the detailed infos for that list element. Also, that scr
Solution 1:
The City
class should implement Parcelable
. Its toString()
method should return the name of the city. (Rather than simply having the city as a String
.)
When you populate your ListView, use an ArrayAdapter<City>
instead of an ArrayAdapter<String>
(this code assumes that the cities are kept in List<City> list
):
City[] cities = new City[list.size()];
list.toArray(cities);
mListView = (ListView) findViewById(R.id.citylist);
mListView.setAdapter(newArrayAdapter<City>(this,
R.layout.listitem, cities));
In your onItemClick handler, get the selected city and add it as an extra on the intent you use to launch your details activity:
mListView.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> a,
View v, int position, long id) {
Citycity= (City) a.getItemAtPosition(position);
Intentintent=newIntent(v.getContext(), DetailsActivity.class);
intent.putExtra("com.example.cities.City", city);
startActivity(intent);
}
});
In your details activity, get the city from the extras on the intent:
Bundlebundle= getIntent().getExtras();
Citycity= bundle.getParcelable("com.example.cities.City");
Post a Comment for "How To View Detailed Data Of A Specific List View Item"