Question

So I have a ListView inside my TabHost and I want to redrow its content every time I change the tab. Nevertheless I never get the list drawed.

My xml file looks like:

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

And my 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
        }
}

The adpter DishSelectorAdapter has been already used in other situation and it works ok, so I don't think it is relevant here.

Thanks by hand.

Was it helpful?

Solution

Well well, it was not as hard as I've thought. The answer was just in front of my eyes.

I didn't put the ListView inside de FrameLayout. The correct code will be:

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

Still rest of the code has some issues to work about, but nothing I coudln't fix with a quick look.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top