0

所以我的 TabHost 中有一个 ListView,我想在每次更改选项卡时重新显示它的内容。尽管如此,我从来没有绘制过列表。

我的 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: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" />
            <!-- Dishes Panel  -->
            <ListView xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:id="@+id/dishSelector"
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        tools:context=".MenuSelectorActivity" />
    </LinearLayout>
</TabHost>

还有我的 TabActivity:

 public class MenuSelectorActivity extends TabActivity implements TabHost.TabContentFactory{    
        private ListView dishSelector;
        private DishSelectorAdapter dishSelectorAdapter;
        private TabHost tabHost;
        private DishDao dishDao;
        private DishTypeDao dishTypeDao;
        private Map<Integer, List<DishPojo>> dishesByType;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu_selector);

            tabHost = getTabHost();  // The activity TabHost
            tabHost.setup();
            TabHost.TabSpec spec;
            dishDao = new DishDao(this);
            dishTypeDao = new DishTypeDao(this);

            dishesByType = new HashMap<Integer, List<DishPojo>>();//new ArrayList<List<DishPojo>>();

            List<DishType> types = dishTypeDao.list();
            TextView dTypeNameView;

            for(DishType dType : types){
                dishesByType.put(dType.getId().hashCode(), createDishPojoList(dishDao.list(
                        new String[]{DishDbContract.SQL_WHERE_CLAUSE_LIST_BY_TYPE}, new String[]{dType.getId()})));
            }
            tabHost.setCurrentTab(0);

            for(DishType dType : types){
                dTypeNameView = new TextView(this);
                dTypeNameView.setText(dType.getDescription());
                spec = tabHost.newTabSpec(dType.getId()).setIndicator(dTypeNameView).setContent(this);
                tabHost.addTab(spec);
            }
        }

        @Override
        public View createTabContent(String tag) {

            dishSelector = (ListView) findViewById(R.id.dishSelector);
            if(dishSelectorAdapter == null){
                dishSelectorAdapter = new DishSelectorAdapter(this, dishesByType.get(tag.hashCode()));
            }else{
                dishSelectorAdapter.setDishToSelect(dishesByType.get(tag.hashCode()));
            }
            dishSelector.setAdapter(dishSelectorAdapter);
            dishSelector.refreshDrawableState();
            return dishSelector;
        }
        private List<DishPojo> createDishPojoList(List<Dish> dishes){
            //creates a list of Pojos
        }
}

适配器 DishSelectorAdapter 已经在其他情况下使用过并且工作正常,所以我认为它在这里不相关。

亲手感谢。

4

1 回答 1

0

好吧,这并没有我想象的那么难。答案就在我眼前。

我没有将 ListView 放在 de FrameLayout 中。正确的代码将是:

<?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" />
            <!-- Dishes Panel  -->
            <ListView xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:id="@+id/dishSelector"
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        tools:context=".MenuSelectorActivity" />
       </FrameLayout>
    </LinearLayout>
</TabHost>

其余代码仍有一些问题需要解决,但我无法通过快速查看来解决。

于 2013-04-14T18:38:43.093 回答