在我正在编写的应用程序中,我有一个扩展 ListActivity 的主类 - 它包含一个元素列表。我希望这些元素链接到不同的页面(xml 页面,或者可能是 View 对象)。但是,我意识到不能在 ListActivity 对象上使用 setContentView(int)
方法。
要做什么?
谢谢!
最佳答案
您似乎正在尝试启动一项新 Activity 。
您必须重写ListActivity
的onListItemClick
方法。
这是代码。
// 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/