Question

I have used TabHost to display 2 tabs. On one of the tabs, there is a custom list. I don't know how to display the same custom list on the other tab, but with different data. I have placed a ListView in the other tab too. But what to do after that? Do I need to write the whole code again? Or do I need to override the OnTabChangedListener method. XML:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ListView
                    android:id="@android:id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true" >
                </ListView>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ListView
                    android:id="@android:id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true" >
                </ListView>

            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

The Code:

public class Contacts extends ListActivity
{
private TabHost thCont;
private String TAB_1_TAG = "tag1";
private String TAB_2_TAG = "tag2";
private int[] imagesId1 = {R.drawable.praneel, R.drawable.saakshi, R.drawable.arjun, R.drawable.rishi, R.drawable.nisarg, R.drawable.ankit, R.drawable.sharad};

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts2);
    thCont = (TabHost) findViewById(R.id.tabhost);
    thCont.setup();
    TabSpec specs;

    // Tab1
    specs = thCont.newTabSpec(TAB_1_TAG);
    specs.setContent(R.id.tab1);
    specs.setIndicator("Managing Team");
    thCont.addTab(specs);

    // Tab 2
    specs = thCont.newTabSpec(TAB_2_TAG);
    specs.setContent(R.id.tab2);
    specs.setIndicator("Heads");
    thCont.addTab(specs);

    setListAdapter(new MyAdapter(Contacts.this, android.R.layout.simple_list_item_1, R.id.textView1, getResources().getStringArray(R.array.contacts_list)));
}

After this there is a subclass which extends ArrayAdapter for the custom list. I have not displayed that part.

Was it helpful?

Solution

[1]: http://androidrises.blogspot.in/2012/10/tabactivity-example.html see this link and make your two tabs as explained in it, and use the same custom adapter for both tabs , if any problem persist then let me know

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