java - 设置由 Activity 链接的新页面

标签 java android

在我正在编写的应用程序中,我有一个扩展 ListActivity 的主类 - 它包含一个元素列表。我希望这些元素链接到不同的页面(xml 页面,或者可能是 View 对象)。但是,我意识到不能在 ListActivity 对象上使用 setContentView(int) 方法。

要做什么?

谢谢!

最佳答案

您似乎正在尝试启动一项新 Activity 。

您必须重写ListActivityonListItemClick 方法。

这是代码。

// ListView l points to list view whose item user clicked
// View v points to the item in the list view on which the user clicked
// int position is the position index of the item in the list
// long id is the id assigned to the item. This id is assigned using the ListAdapter, CursorAdapter etc.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // I am using getApplicationContext() as it is more safe then just passing `this`
    Intent i = new Intent(this.getApplicationContext(), ActivityToRun.class);

    this.startActivity(i);
}

注意:您必须根据您的需要改进此框架。

关于java - 设置由 Activity 链接的新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8337511/

相关文章:

android - 如何在 Android Studio 中获取最新的通话记录?

java - Android 相机,地理标记上的秒数四舍五入为整数

android - 摩艾安卓构建问题

java - Android:让服务定期检查某些内容

java - 显示 mvn 测试期间错误的完整跟踪

java - 在 Java 中将 arp -a 输出解析为行和列

java - 在我的 Java 类中使用 Sesame 2 和 MySQL

java - Intellij 无法解析导入时的符号

java - 无法连接到JReport中的数据库

android - 即时应用程序 : Use feature module from base module without strong dependency