在 Android 中更改活动 - 选项卡内的列表
嘿,我一直在堆栈溢出中搜索与此相同的问题,但我似乎无法使其工作,我迫切希望有人可以提供帮助!
我有三个选项卡,每个选项卡都有自己的活动。在第一个选项卡中,我在选项卡中实现了一个列表,它有一些显示没有问题的值。另外两个选项卡具有表格布局。
问题是这样的: 当单击任何列表项时,我需要切换到第二个选项卡。
我该如何执行此操作?我已经在互联网上阅读了几页有关注册意图的页面,但似乎无法使用实现 listView 的 tabHost 进行任何操作。
代码如下!
非常感谢。
公共类 AlertsActivity 扩展 TabActivity 实现 OnTabChangeListener、OnClickListener { private static Final String LIST1_TAB_TAG = "已保存的警报"; 私有ListView listView1; 私有 TabHost tabHost;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
listView1 = (ListView) findViewById(R.id.list);
List<String> listStrings1 = new ArrayList<String>();
listStrings1.add("Item 1");
listStrings1.add("Item 2");
listStrings1.add("Item 3");
listStrings1.add("Item 4");
listStrings1.add("Item 5");
listStrings1.add("Item 6");
listStrings1.add("Item 7");
listStrings1.add("Item 8");
listStrings1.add("Item 9");
listStrings1.add("Item 10");
listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listStrings1));
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
}
public void onTabChanged(String tabName) {
if(tabName.equals(LIST1_TAB_TAG)) {
}
}
public void onClick(View src) {
}
}
Hey there, I've been searching stack overflow for the same question as this but I cannot seem to make it work and I am desperately hoping someone can help!
I have got three tabs, each with their own activity. In the first tab I have implemented a list within the tab and it has some values which display no problem. The other two tabs have table layouts.
Here's the issue:
I need to switch to the second tab when any of the list items are clicked.
How do I do this? I have read several pages on the internet about registering intent, but can't seem to make anything work with a tabHost implementing a listView.
Code below!
Many thanks.
public class AlertsActivity extends TabActivity implements OnTabChangeListener, OnClickListener {
private static final String LIST1_TAB_TAG = "Saved Alerts";
private ListView listView1;
private TabHost tabHost;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
listView1 = (ListView) findViewById(R.id.list);
List<String> listStrings1 = new ArrayList<String>();
listStrings1.add("Item 1");
listStrings1.add("Item 2");
listStrings1.add("Item 3");
listStrings1.add("Item 4");
listStrings1.add("Item 5");
listStrings1.add("Item 6");
listStrings1.add("Item 7");
listStrings1.add("Item 8");
listStrings1.add("Item 9");
listStrings1.add("Item 10");
listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listStrings1));
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
}
public void onTabChanged(String tabName) {
if(tabName.equals(LIST1_TAB_TAG)) {
}
}
public void onClick(View src) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的侦听器中(在您的 TabActivity 中)只需执行以下操作:
TabHost 文档 setCurrentTab(int)
getTabHost() 的 TabActivity 文档
In your listener, (within your TabActivity) just do:
TabHost documentation for setCurrentTab(int)
TabActivity documentation for getTabHost()