开发者

Help with Tab OnClickListeners android

I am having some trouble creating an application that downloads data from server. What I would like to do is download some data when a user clicks on a tab from within a tab host. The idea is that the next activity that the tab points to will then use the data to populate a list view. I've been trying to use an onClickListener for the tabs, but It doesn't seem to work. I am attaching what I have thus far. I'd like to call the method performGetClasses(), when a user clicks on the tab labeled TAB_NAME_2.

Thanks in advance.

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.a开发者_C百科ddTab(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));
        }
 }


Why not put the code in your onCreate of your classes intent ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜