帮助Tab OnClickListeners android

时间:2011-01-03 21:24:54

标签: java android tabs

我在创建从服务器下载数据的应用程序时遇到了一些问题。我想要做的是当用户从标签主机中点击标签时下载一些数据。我们的想法是,标签指向的下一个活动将使用数据填充列表视图。我一直在尝试使用onClickListener作为选项卡,但它似乎不起作用。我附上了迄今为止的内容。当用户点击标有TAB_NAME_2的标签时,我想调用方法performGetClasses()。

提前致谢。

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     try {
      FileInputStream fis = openFileInput("token");
      try {
       fis.read(tokenInt);
       token = new String(tokenInt);
       fis.close();
      } catch (IOException e) {
      }
     } catch (FileNotFoundException e) {

     }

     //TODO: Add code to send the token as a put extra to each tab, rather than retrieving in each separate activity. 

     setContentView(R.layout.home_screen);

     Resources res = getResources(); 
     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec;  
     Intent intent; 

     // Do the same for the other tabs
     intent = new Intent().setClass(this, HomePage.class);
     spec = tabHost.newTabSpec("TAB_NAME_1").setIndicator("Home",res.getDrawable(R.layout.ic_tab_home)).setContent(intent);
     tabHost.addTab(spec);


     intent = new Intent().setClass(this, ClassesPage.class);
     spec = tabHost.newTabSpec("TAB_NAME_2").setIndicator("Classes",res.getDrawable(R.layout.ic_tab_classes)).setContent(intent);
     tabHost.addTab(spec);

     intent = new Intent().setClass(this, SearchPage.class);
     spec = tabHost.newTabSpec("TAB_NAME_3").setIndicator("Search",res.getDrawable(R.layout.ic_tab_search)).setContent(intent);
     tabHost.addTab(spec);

     intent = new Intent().setClass(this, MessagesPage.class);
     spec = tabHost.newTabSpec("TAB_NAME_4").setIndicator("Messages",res.getDrawable(R.layout.ic_tab_messages)).setContent(intent);
     tabHost.addTab(spec);

     intent = new Intent().setClass(this, AccountPage.class);
     spec = tabHost.newTabSpec("TAB_NAME_5").setIndicator("Account",res.getDrawable(R.layout.ic_tab_account)).setContent(intent);
     tabHost.addTab(spec);

     tabHost.setCurrentTab(0);
 }

 public void performGetClasses(String token){

  progressDialog = ProgressDialog.show(HomeScreen.this,
                "Please wait...", "Retrieving data...", true, true);

        if (!(token == null)) {
         PerformClassesTask task = new PerformClassesTask();
         task.execute(token);
            progressDialog.setOnCancelListener(new CancelTaskOnCancelListener(task));
        }
 }

1 个答案:

答案 0 :(得分:0)

为什么不把代码放在你的类的onCreate意图?