Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
olix20 Avatar asked Feb 25 '23 13:02

olix20


2 Answers

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);

like image 71
Stephan Avatar answered Mar 08 '23 04:03

Stephan


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);
like image 21
Bluefish Avatar answered Mar 08 '23 05:03

Bluefish