2012-02-12 47 views
0

我试图插入一个ListView作为Tabhost中的一个选项卡。我浏览了网页,发现有几个实现说他们工作,但不适合我(无论是在他们自己的项目中,还是在合并到我的项目中)。把列表视图放在TabHost(作为一个视图,而不是一个活动)时遇到困难

我已经解决了以下问题,它可以工作(至少不会崩溃),并且我在LogCat中没有收到任何错误,但该选项卡显示为空。

我已经检查了提供列表(tooldisplay)的数组并且它已被填充。

我宁愿不必触发另一个活动来填充选项卡。

setupdetail.xml:为tabhost

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

活动(编辑以去除不相关的代码):

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)); 
    } 
} 
+0

你有没有试着用列表替换的FrameLayout和设置列表是@android上的ID:ID/tabcontent? – jsmith 2012-02-12 18:35:42

+0

我为什么要那样做?根据文档,你必须有framelayout包含标签... – Barak 2012-02-12 18:41:00

回答

0

,我发现我的问题的根源后,更使然。为以后出现同样问题的任何人提供答案。

我正在使用错误的构造函数。由于我在布局中有不止一个textview,我需要指定布局和适配器的textview(请注意,我修改的代码与最初发布时的代码稍有不同,因为我切换到自定义列表布局,但问题仍然存在,直到我遇到构造函数修复)。

mListView.setAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, tooldisplay)); 

应该是:

ListView mListView = (ListView)findViewById(R.id.list1);   
    mListView.setAdapter(new ArrayAdapter<String>(this,   
      R.layout.listlayout, R.id.ListItem1, tooldisplay));   
相关问题