从 ListView 启动 Activity
我的列表包括位于“标题”行下方的八个项目,全部包含在我的应用程序的选项卡中。我无法弄清楚如何根据列表中单击的项目更改为活动(或可能是不同的选项卡)。
我目前正在扩展 Activity 类,不确定这是否是一个问题。我尝试在扩展 ListActivity 类的同时使用 onListItemClick ;但这导致应用程序崩溃。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class Tab2 extends Activity {
private ListView listView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
Cinema cinema_data[] = new Cinema[]{
new Cinema(R.drawable.blue, "Blue Cinema"),
new Cinema(R.drawable.green, "Green Cinema"),
new Cinema(R.drawable.purple, "Purple Cinema"),
new Cinema(R.drawable.red, "Red Cinema"),
new Cinema(R.drawable.yellow, "Gold Cinema"),
new Cinema(R.drawable.blue, "Cyan Cinema"),
new Cinema(R.drawable.green, "Lime Cinema"),
new Cinema(R.drawable.purple, "Magenta Cinema")
};
CinemaAdapter adapter = new CinemaAdapter(this,
R.layout.listview_item_row, cinema_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
任何帮助将不胜感激!
编辑:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
Intent i = new Intent("android.lab.two.Tab1");
startActivity(i);
} catch(Exception e){
e.printStackTrace();
}
}
My list includes eight items positioned beneath a 'header' row, all contained within a tab on my application. I cannot figure out how to change to an activity (or possibly a different tab) based upon the item clicked on in the list.
I'm currently extending the Activity class, not sure if this is an issue. I've attempted to use the onListItemClick whilst extending the ListActivity class; this however caused the application to crash.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class Tab2 extends Activity {
private ListView listView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
Cinema cinema_data[] = new Cinema[]{
new Cinema(R.drawable.blue, "Blue Cinema"),
new Cinema(R.drawable.green, "Green Cinema"),
new Cinema(R.drawable.purple, "Purple Cinema"),
new Cinema(R.drawable.red, "Red Cinema"),
new Cinema(R.drawable.yellow, "Gold Cinema"),
new Cinema(R.drawable.blue, "Cyan Cinema"),
new Cinema(R.drawable.green, "Lime Cinema"),
new Cinema(R.drawable.purple, "Magenta Cinema")
};
CinemaAdapter adapter = new CinemaAdapter(this,
R.layout.listview_item_row, cinema_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
any help will be greatly appreciated!
edit:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
Intent i = new Intent("android.lab.two.Tab1");
startActivity(i);
} catch(Exception e){
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果 OnItemClickListener 不起作用,您可以尝试在适配器中设置它:
在适配器的 getView 方法中,您可以将 OnClickListener 添加到行视图,如下所示:
if the OnItemClickListener doesn't work, you can try setting it in your adapter :
in your adapter, in the
getView
method, you can add an OnClickListener to your row view like this :您只需在列表视图上设置点击监听器即可。你得到这个位置并从那里执行你的逻辑。下面的代码应该可以帮助您入门。我不知道你遇到了什么错误,但不会出错。
You just set the click listener on your listview. You get the position and perform you logic from there. The code below should help you get started. I don't know what error you were getting, but there is not much that can go wrong.
我希望这有帮助..
I hope this helps..
您无法从 TabHost 启动活动。你在一个内部类中。您必须使用嵌套类。
不要在 onClickListener 中启动 Intent,而是尝试记录一些内容,您会发现它可以工作。
Log.d("LOG","消息");
You cannot start an activity from a TabHost. You are in a inner class. You must use NestedClasses.
Instead of starting an Intent in your onClickListener, try to Log something, and you will see that will work.
Log.d("LOG","Message");
如果您在选项卡(活动)中设置了 onListItemClick() 方法,那么您必须使用
如果您设置 onListItemClick() 方法是在其他类中定义的,那么您必须使用
如果 tab1(活动) 是主活动并且您必须调用 tab2 (活动)在 tab1(活动)内,那么你必须使用这种方式......
if you set onListItemClick() method inside your tab(activity) then you have to use
if you set onListItemClick() method is define in other class then you have to use
if tab1(activity) is a main activity and you have to call tab2(activity) inside the tab1(activity) then you have to use this way...