Thursday, April 12, 2012

How to populate listview adapter from outside Activity in a TabActivity

I have the following problem.



I am in a tabActivity with 2 tabs. Every tab will launch the same activity, but there is a onTabChangeListener.
In this tabchange listener i need to set up the listview adapter that is different for each activity.
So i will have two different lists, and i want to change their adapter from TabActivity, from this onTabchangeListener, but i get the following error: system services not available to activities before oncreate()
The code for tabs is:



  host = getTabHost();

host.addTab(host.newTabSpec(TAG_AVAILABLE).setIndicator("First Tab")
.setContent(new Intent(this, MyActivity.class)));
host.addTab(host.newTabSpec(TAG_DOWNLOAD).setIndicator("Second Tab ")
.setContent(new Intent(this, MyActivity.class)));
host.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
MyActivity myActivity = new MyActivity();
int i = getTabHost().getCurrentTab();
if (i == FIRST_TAB) {
setAdapterForMyActivity(firstAdapter);
}
}
if (i == SECOND_TAB) {
setAdapterForMyActivity(secondAdapter);
}
}
}
});

}


The main question is how to set adapter of an listView which is in an Activity from TabActivity.
Thank you very much.





No comments:

Post a Comment