显示在Android的一个ListView选择一个项目后,一个新的列表? [英] Displaying a new list after selecting an item from a ListView in Android?

查看:155
本文介绍了显示在Android的一个ListView选择一个项目后,一个新的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Android。我已经能够成功地显示一个列表,并在任何具体项目处理点击。出现的基础上被点击了什么项目什么我想要做的就是有一个新的列表。

I'm just starting with Android. I've been able to successfully display a list and handle clicks on any particular item. What I want to do is have a new list appear based on what item is clicked.

例如..

Florida
Georgia
....

用户点击佛罗里达州,并显示一个新的列表...

User clicks Florida and a new list is displayed...

Miami
Tampa
..

我应该创建一个新的活动和处理这样的说法?出于某种原因,我无法找到相当样品code,涵盖了这种情况。

Should I create a new Activity and handle it that way? For some reason I just can't quite find sample code that covers this scenario.

推荐答案

最常见的方式是有其他活动。在你onItemClick处理程序创建一个Intent,传递你点击的项目,然后启动这一点。

Most common way is to have another activity. In your onItemClick handler you create an Intent, pass in the item you clicked on, and then launch that.

因此​​,像

ListView listView = new ListView(this);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Object obj = listView.getAdapter().getItem(arg2);
    Intent in = new Intent(FirstListActivity.this, SecondListActivity.class);
    in.putExtra("obj", obj );
    startActivity(in);
}

});

然后在你的第二个活动,您可以获取对象点击,然后用它来填充另一个列表视图。这样做的好处是,用户可以使用undo按钮回到第一个列表很好。

Then in your 2nd activity, you can get the object clicked on, and then use that to populate another list view. The advantage of this is that the user can use the undo button to go back to the first list nicely.

记住,你必须通过实现Serializable接口或者是一个基本类型的对象。

Remember the object you are passing must implement Serializable or be a basic type.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Object obj  = getIntent().getSerializableExtra("obj");
    ...

这篇关于显示在Android的一个ListView选择一个项目后,一个新的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆