如何查看特定列表视图项的详细数据 [英] 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 screen will have others buttons ( eg: Write Review, Post Picture )
我怎样才能做到这一点?
How can i achieve this ?
推荐答案
在城
类实现 Parcelable
。其的toString()
方法应该返回一个城市的名称。 (而不是简单地具有城市为字符串
)
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
.)
在填充您的ListView,使用 ArrayAdapter<市>
代替 ArrayAdapter<字符串>
(此code假定城市都保存在名单,其中,城市与GT;清单
):
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(new ArrayAdapter<City>(this,
R.layout.listitem, cities));
在你onItemClick处理,获得所选择的城市并将其添加为一个额外的用于启动您的详细信息活动的意图:
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(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a,
View v, int position, long id) {
City city = (City) a.getItemAtPosition(position);
Intent intent = new Intent(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:
Bundle bundle = getIntent().getExtras();
City city = bundle.getParcelable("com.example.cities.City");
这篇关于如何查看特定列表视图项的详细数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!