olix20
olix20

Reputation: 794

TabHost not showing contents in the beginning

I have a TabActivity with two tabs. the layout of the activity is as follows:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:padding="5dp">
        <ListView android:id="@+id/list"
            android:layout_width="fill_parent" android:layout_height="fill_parent" />

    </FrameLayout>
</LinearLayout>
</TabHost>

The layout consists of a ListView which is populated accordingly in setOnTabChangedListener(). no problem with filling the list and showing it.

My problem is that the list view is not shown when the activity starts, although I find it by ID and populate it; it is only populated after I change the tabs.

and the code in the onCreate(..) looks like this:

l = (ListView) findViewById(R.id.list);
l.setAdapter(new CommentsAdapter(this, (JSONArray) obj));

TabSpec spec;
    spec = tabHost.newTabSpec("0").setIndicator("0",
            res.getDrawable(R.drawable.tab_recent)).setContent(
            R.id.list);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("1").setIndicator("1",
            res.getDrawable(R.drawable.tab_pop)).setContent(
            R.id.list);
    tabHost.addTab(spec);

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            int tab = Integer.valueOf(tabId);
            showDialog(WAIT_DIALOG);

            switch (tab) {
            // recent
            case 0:
                //refill the list
                break;
            // popular

            case 1:
                //refill the list
                break;
            }
        }
    });

    tabHost.setCurrentTab(0);

any hints?

Upvotes: 4

Views: 2905

Answers (3)

Bluefish
Bluefish

Reputation: 11

You can try use different layout, say

spec = tabHost.newTabSpec("0").setIndicator("0",
        res.getDrawable(R.drawable.tab_recent)).setContent(
        R.id.list1);
tabHost.addTab(spec);

spec = tabHost.newTabSpec("1").setIndicator("1",
        res.getDrawable(R.drawable.tab_pop)).setContent(
        R.id.list2);
tabHost.addTab(spec);

Upvotes: 1

Stephan
Stephan

Reputation: 1868

I had a similar issue. What I noticed is that if you do

tabHost.setCurrentTab(0);

then onTabChanged is not triggered. BUT, if you do

tabHost.setCurrentTab(1);

then it is, the list shows up and all it's fine. As per why this happens, it remains a bit of a mistery to me.

Use this cheat to start your app on the first tab: in onCreate(), after you've created the tabs call first: tabHost.setCurrentTab(1); Then call: tabHost.setCurrentTab(0);

Upvotes: 9

Sumant
Sumant

Reputation: 2795

you just do another activity in which list view is present & put the below one

Intent i1 = new Intent(tabs.this,listDisplay.class);
spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable.                        tab_recent)).setContent(i1);                                         

Also put the tabHost.setCurrentTab(0); before ontabChangeListener Hope this might help..

hope this might solve the problem..

Upvotes: -2

Related Questions