我在创建一个从服务器下载数据的应用程序时遇到了一些问题。我想要做的是当用户单击选项卡主机中的选项卡时下载一些数据。其思想是,选项卡所指向的下一个活动将使用数据来填充列表视图。我一直在尝试使用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));
}
}发布于 2011-01-04 07:07:31
为什么不把代码放在你的类的onCreate中呢?
https://stackoverflow.com/questions/4588506
复制相似问题