sherry
sherry

Reputation: 1859

creating tab content on demand in android apps

I have to tabs, new and REPORTS. In the REPORTS tab, I'm creating a listview that is created dynamically. Now listview list items are being fetched from the database. So every time the REPORTS tab is loaded, the listview list items have to be fetched from the database in case there has been a change in the list items. If I put all this stuff in the onCreate of REPORTS tab, everything works perfectly. But if I switch to another tab and switch back to REPORTS tab, the listview is populated but the change made in the database in the list items is not reflected. Whenever I come back to the REPORTS tab, I need to get the current data from the database and display in the listview.

So the problem is since I'm doing the populating of the listview in onCreate, this is happening only once. After looking around online, I found the TabContentFactory() stuff. Well, I thought this would solve my problem. This is what I did,

TabHost.TabSpec reportsSpec = tabHost.newTabSpec("reports").setIndicator("Reports")
    .setContent(new TabHost.TabContentFactory(){
        public View createTabContent(String tag) {
            Context context = Main_screen.this;
            ls1 = new ListView(Main_screen.this); 
            //populate m_data from the database

            CustomAdapter adapter =  new CustomAdapter(context, m_data);
            ls1.setAdapter(adapter);    
            return ls1;}});
tabHost.addTab(reportsSpec);

But this doesn't solve my problem. It looks like the code inside createTabContent is executed only when the tab is created and not every time I switch to the REPORTS tab.

Can this be done at all? Can we create the content of a tab every time we switch to the tab? Please help. Any help is greatly appreciated.

Upvotes: 0

Views: 578

Answers (1)

BZ.
BZ.

Reputation: 1946

In the tabview, try putting the data load into the onResume method.

Upvotes: 1

Related Questions