Android TabActivity如何仅在TabChange上启动Intent

时间:2012-11-21 12:50:22

标签: android android-intent tabactivity tabview

我的代码存在问题:

我想使用Google AppEngine创建一个Highscore列表并且它有效。 Google AppEngine返回一个String,该String在Layout中解析并填充。 活动HighscoreListe连接到AppEngine并将获得此字符串。

我的Highscore布局问题与我下面编写的方式有关,当我启动TabLayout时,此活动(HighscoreListe)会多次启动。所以我得到一个StringIndexOutOfBoundsException。

当我更改为Tab 2时,我想启动intent2,当我更改为Tab3时,我想启动intent3。 因此,当我点击这个难度时,高分将只会加载到第二或第三难度。

public class HighscoreTabLayout extends TabActivity{

@Override
   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       TabHost tabHost = getTabHost();

       System.out.println(MenuMainActivity.getDifficulties());
       int diffCount = -1;
       System.out.println(diffCount);

       TabSpec tabSpec1 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
       // setting Title and Icon for the Tab
       tabSpec1.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_photos_tab));
       Intent intent1 = new Intent(this, HighscoreListe.class);
       intent1.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
       tabSpec1.setContent(intent1);
       System.out.println(diffCount);

       TabSpec tabSpec2 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
       tabSpec2.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_songs_tab));
       Intent intent2 = new Intent(this, HighscoreListe.class);
       intent2.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
       tabSpec2.setContent(intent2);
       System.out.println(diffCount);

       TabSpec tabSpec3 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
       tabSpec3.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_videos_tab));
       Intent intent3 = new Intent(this, HighscoreListe.class);
       intent3.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
       tabSpec3.setContent(intent3);
       System.out.println(diffCount);

       TabSpec tabSpec4 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
       tabSpec4.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_videos_tab));
       Intent intent4 = new Intent(this, HighscoreListe.class);
       intent4.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
       tabSpec4.setContent(intent4);
       System.out.println(diffCount);

       // Adding all TabSpec to TabHost
       tabHost.addTab(tabSpec1);
       tabHost.addTab(tabSpec2);
       tabHost.addTab(tabSpec3);
       tabHost.addTab(tabSpec4);

       tabHost.setCurrentTab(1);


}

}

1 个答案:

答案 0 :(得分:0)

如果我明白你的意思,你应该在tabhost上设置tab chage listner:

`TabHost.setOnPageChangeListener(new OnTabChange Listener(){

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub

        }
    })`