帮助 Tab OnClickListeners android
我在创建从服务器下载数据的应用程序时遇到一些问题。我想做的是当用户单击选项卡主机中的选项卡时下载一些数据。这个想法是,选项卡指向的下一个活动将使用数据来填充列表视图。我一直在尝试对选项卡使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将代码放在类意图的 onCreate 中?
Why not put the code in your onCreate of your classes intent ?