4

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?

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
olix20
  • 794
  • 1
  • 11
  • 23

3 Answers3

9

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

Community
  • 1
  • 1
Stephan
  • 1,858
  • 2
  • 25
  • 46
  • This may work if my second tab didnt try to update its content before the the first is executed, `java.lang.IllegalStateException: Content view not yet created` – Mike Flynn Mar 17 '14 at 03:09
  • @MikeFlynn without a little more context I'm not sure how to reply – Stephan Mar 17 '14 at 17:35
  • 1
    I am facing this problem, did anyone get a solution even after 5 years? – Imdad Jul 22 '16 at 05:59
1

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);
Bluefish
  • 11
  • 1
-2

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..

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Sumant
  • 2,775
  • 2
  • 22
  • 30