Setting new pages linked by an Activity

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

In the application I am writing, I have a main class which extends the ListActivity - it contains a list of elements. I want these elements to link to a different page (an xml one, or perhaps a View object). However, I realised one cannot use the method setContentView(int) on a ListActivity object.

What's to be done?

Thanks!

回答1:

Looks like you are trying to launch a new activity.

You have to override the onListItemClick method of ListActivity.

Here is the code.

// 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); }

NOTE: You have to improve on this skeleton depending upon your needs.



回答2:

Have you tried setting the XML page as a ListView and give it the ID @android:id/list?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!