I’m trying to insert a ListView as one of the tabs in a tabhost. I’ve scoured the web and found several implementations that say they work, but don’t for me (either in their own projects or when incorporated into mine).
I’ve settled on the following, which works (as far as not crashing) and I’m not getting any errors in LogCat, but the tab appears empty.
I’ve checked the array supplying the list (tooldisplay) and it is populated.
I would rather not have to fire another activity to populate the tab.
setupdetail.xml:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/setupheader"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="This will be the setup header"
android:textSize="15dp" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="bottom" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<!-- General Info Tab -->
<LinearLayout
android:id="@+id/general_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<!-- Tool Tab -->
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</FrameLayout>
</LinearLayout>
</TabHost>
activity for tabhost (edited to remove non-relevant code):
public class SetupDisplay extends TabActivity {
private String[] tooldisplay = new String[20];
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setupdetailmain);
// Set up tabs
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("general").setIndicator("General")
.setContent(R.id.general_tab);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tools").setIndicator("Tools")
.setContent(R.id.list1);
tabHost.addTab(spec);
// Load data into tooldisplay[]
// get view & set adapter
mListView = (ListView)findViewById(R.id.list1);
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tooldisplay));
}
}
I found the source of my problem after much more googling. Posting an answer for anyone who might come later with the same issue.
I was using the wrong contructor.
Since I have more than a textview in the layout, I need to specify the layout AND the textview for the adapter (Note my code for the fix is a little different from originally posted as I switched to a custom list layout, but the issue was still there until I came upon the constructor fix).
should be: