Q:

Setting ListView two times in Fragment Class

I have a basic table in the database called 'Category'. Then other two tables called Category1 and Category2 both have one-to-many relationship with category table. Its like this:-
**Category Table**
Id    Category
0     Category1 (One) -> Category1 Table (Many).
1     Category2 (One) -> Category2 Table (Many).

I am using a Tab Manager which will show the both categories from the category table and the list of items in a listview in one tab from the category1 or category2 table and the other one in the other tab. But the problem I am facing is that I am trying to set the adapter on listview based on an if-else statement. So its only showing category1 items and not category2 items when I press on category2 on the tab.
I know that I cant set two adapters in a single listview based on the research I have done. Is there any solution? Below is my code.
FragmentClass
public class CategoryFragment extends Fragment{

    private ListView listView;
    private String currentTabTag;
    private CategoryAdapter1 categoryAdapter1;
    private CategoryAdapter2 categoryAdapter2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_category_fragment_list, container, false);

        // get references to widgets
        listView = (ListView) view.findViewById (R.id.prescriptorsListView);

        // get the current tab
        TabHost tabHost = (TabHost) container.getParent().getParent();
        currentTabTag = tabHost.getCurrentTabTag();

        // refresh the category list view
        refreshCategoryList();

        // return the view
        return view;
    }

    public void refreshCategoryList() {
        // get category list for current tab from database
        Context context = getActivity().getApplicationContext();
        CategoryDatabase categoryDatabase = new CategoryDatabase(context);

        if(currentTabTag.equals("Category 1")) {
            ArrayList<Category1> category1ArrayList = categoryDatabase.getAllCategory1(currentTabTag);
            // create adapter and set it in the ListView widget
            listView = new Category1Adapter(context, category1ArrayList);
            listView.setAdapter(Category1Adapter);
        }
        else if(currentTabTag.equals("Category 2"))
        {
            ArrayList<Category2> category2ArrayList = categoryDatabase.getAllCategory2(currentTabTag);
            // create adapter and set it in the ListView widget
            listView = new Category2dapter(context, category2ArrayList);
            listView.setAdapter(Category2Adapter);
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        refreshTaskList();
    }
}

Adapter of Category1
public class Category1Adapter extends BaseAdapter {
    private Context context;
    private ArrayList<Category1> category1ArrayList;

    public Category1Adapter(Context context, ArrayList<FamilyPrescription> category1ArrayList) {
        this.context = context;
        this.category1ArrayList = category1ArrayList;
    }

    @Override
    public int getCount() {
        return category1ArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return category1ArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Category1TaskLayout taskLayout = null;
        Category1 category1 = category1ArrayList.get(position);

        if (convertView == null) {
            taskLayout = new Category1TaskLayout(context, category1);
        } else {
            taskLayout = (Category1TaskLayout) convertView;
            taskLayout.setCategory1(category1);
        }
        return taskLayout;
    }
}

The adapter of category2 is similar to above adapter.
Any help would be appreciated :)

A:

As i suggested, i thought about using one adapter.
private class OneAdapter extends BaseAdapter {

    List<Category1> cat1 = categoryDatabase.getAllCategory1(currentTabTag);
    List<Category2> cat2 = categoryDatabase.getAllCategory2(currentTabTag);

    @Override
    public int getCount() {
        if(currentTabTag.equals("Category 1")) {
            return cat1.size();
        } else if(currentTabTag.equals("Category 2")) {
            return cat2.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(currentTabTag.equals("Category 1")) {
            return cat1.get(position);
        } else if(currentTabTag.equals("Category 2")) {
            return cat2.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //...

        Object c = getItem(position);
        if(c instanceof Category1) {
            ...
        } else if(c instanceof Category2) {
            ...
        }

        return convertView;
    }
}

As tab changed need to update adapter;
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            currentTabTag = tabHost.getCurrentTabTag();
            adapter.notifyDataSetChanged();
        }
    });

Full working example in Activity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    TabHost tabHost;
    ListView listView;
    String currentTabTag;
    CategoryDatabase categoryDatabase;

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

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        TabHost.TabSpec tab1 = tabHost.newTabSpec("Category 1");
        TabHost.TabSpec tab2 = tabHost.newTabSpec("Category 2");
        tab1.setContent(R.id.tab1);
        tab1.setIndicator("Cat1");
        tab2.setContent(R.id.tab2);
        tab2.setIndicator("Cat2");
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);

        categoryDatabase = new CategoryDatabase();

        currentTabTag = tabHost.getCurrentTabTag();

        listView = (ListView) findViewById(android.R.id.list);

        listView.setAdapter(new TabAdapter());
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                currentTabTag = tabHost.getCurrentTabTag();
                ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
            }
        });
    }

    private class TabAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if(currentTabTag.equals("Category 1")) {
                return categoryDatabase.getAllCategory1().size();
            } else if(currentTabTag.equals("Category 2")) {
                return categoryDatabase.getAllCategory2().size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if(currentTabTag.equals("Category 1")) {
                return categoryDatabase.getAllCategory1().get(position);
            } else if(currentTabTag.equals("Category 2")) {
                return categoryDatabase.getAllCategory2().get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if(convertView == null) {
                convertView = LayoutInflater.from(Main2Activity.this).inflate(android.R.layout.simple_list_item_1, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Object c = getItem(position);
            if(c instanceof Category1) {
                Category1 c1 = (Category1) c;
                holder.text1.setText(c1.text);
            } else if(c instanceof Category2) {
                Category2 c2 = (Category2) c;
                holder.text1.setText(c2.text);
            }

            return convertView;
        }

        class ViewHolder {
            TextView text1;
            public ViewHolder(View convertView) {
                text1 = (TextView) convertView.findViewById(android.R.id.text1);
            }
        }
    }
}

class CategoryDatabase {
    private List<Category1> cat1;
    private List<Category2> cat2;
    {
        cat1 = new ArrayList<Category1>();
        cat2 = new ArrayList<Category2>();
        for(int i=0;i<10;i++) {
            cat1.add(new Category1("Category 1 "+i));
        }
        for(int i=0;i<20;i++) {
            cat2.add(new Category2("Category 2 "+i));
        }
    }

    public List<Category1> getAllCategory1() {
        return cat1;
    }
    public List<Category2> getAllCategory2() {
        return cat2;
    }
}

class Category1 {
    String text;
    public Category1(String text) {
        this.text = text;
    }
}

class Category2 {
    String text;
    public Category2(String text) {
        this.text = text;
    }
}

Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            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="match_parent"
                android:layout_height="match_parent">

                <View
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" />

                <View
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"></View>
            </FrameLayout>
        </LinearLayout>

    </TabHost>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />
</LinearLayout>

