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()。基本上,当我单击列表中的一个项目时,我想转到第一个选项卡。请帮忙

一种方法是使用意图。有很多方法可以使用意图。这在某些情况下尤其有效,例如,活动正在显示来自内容提供商的某些内容。我将在下面描述一种可能的方法:

  • 对于承载选项卡的活动,如果还没有意图过滤器,则为其提供一个意图过滤器。例如,如果它是一个“item detail”样式的活动,可能它的意图过滤器可以匹配某个特定内容类型(如
    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
  • 注意:我自己还没有试过,但理论上应该可以