Dany's
Dany's

Reputation: 928

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.

Upvotes: 1

Views: 214

Answers (1)

Femi
Femi

Reputation: 64700

If you want to pass an Adapter to a child activity, you'll need a global shared object of some kind. One option would be to pass the adapter to an Application object in the parent TabActivity and then retrieve that in the child activity.

Upvotes: 2

Related Questions