帮助 Tab OnClickListeners android

发布于 2024-10-10 01:21:23 字数 2429 浏览 0 评论 0原文

我在创建从服务器下载数据的应用程序时遇到一些问题。我想做的是当用户单击选项卡主机中的选项卡时下载一些数据。这个想法是,选项卡指向的下一个活动将使用数据来填充列表视图。我一直在尝试对选项卡使用 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));
        }
 }

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.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));
        }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2024-10-17 01:21:23

为什么不将代码放在类意图的 onCreate 中?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文