Android 在ListActivity中使用onClickItem更改选项卡
有人能告诉我如何通过点击标签内的元素来更改标签吗?我已经用全球数据试过了。代码如下所示:Android 在ListActivity中使用onClickItem更改选项卡,android,Android,有人能告诉我如何通过点击标签内的元素来更改标签吗?我已经用全球数据试过了。代码如下所示: public class Tabs extends TabActivity { int tabNumber = 0; private TabHost tabHost; int returnedTabNumber = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle save
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
全局适配器如下所示:
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
现在,当我试图通过单击其中一个项目来更改我的ListActivity选项卡中的选项卡时,它不会做任何事情,而是停留在ListActivity选项卡上。也许我不应该在这里使用onResume()。基本上,当我单击列表中的一个项目时,我想转到第一个选项卡。请帮忙 一种方法是使用意图。有很多方法可以使用意图。这在某些情况下尤其有效,例如,活动正在显示来自内容提供商的某些内容。我将在下面描述一种可能的方法:
vnd.android.cursor.item/vnd.somecontent
)上的ACTION\u VIEW
focus\u选项卡
,并在活动的onCreate
中,设置选项卡后,将活动选项卡设置为此focus\u选项卡
额外指定的选项卡onNewIntent
,在该方法中,查找相同的focus\u选项卡
extra,并将活动设置为该extra指定的值focus\u选项卡
extra。添加FLAG\u ACTIVITY\u SINGLE\u TOP
Intent标志,这样就不会创建主机活动的新实例。然后,根据刚才创建的意图调用startActivity