How to return result from Tabs to my Activity

203 Views Asked by At

I am developing an application using android 2.1. In one Page, upper part have two textviews and lower part have tablayout having two tabs.

My XML code is like this...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include  android:id="@+id/content" layout="@layout/content_table" 
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/> 

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost" android:layout_below="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <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="wrap_content" />
    </LinearLayout>
</TabHost> 

</RelativeLayout>

And activity class is like...

public class PageActivity extends ActivityGroup {
    LogoBarActivity logoBar;
    TabHost mTabHost;
    TextView text1;
    TextView text2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.page);

        text1 = findViewById(R.id.textview1);
        text2 = findViewById(R.id.textview2);

        mTabHost = (TabHost) findViewById(R.id.tabHost);  
        mTabHost.setup(this.getLocalActivityManager());
        TabHost.TabSpec spec;  
        Intent intent;  

        intent = new Intent().setClass(this, Type1Activity.class);
        spec = mTabHost.newTabSpec("type1").setIndicator("Type1").setContent(intent);
        mTabHost.addTab(spec);

        intent = new Intent().setClass(this, Type2Activity.class);
        spec = mTabHost.newTabSpec("type2").setIndicator("Type2").setContent(intent);
        mTabHost.addTab(spec);

        mTabHost.setCurrentTab(0);
        mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height /= 2; 
        mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height /= 2;   
    }
}

Explanation: When user comes this page, he can click on any tabs either on Type1 or Type2. Both Tabs have ListView (Type1Activity & Type2Activity are ListView ). When user click on any tab he will get ListView in Tabs. User can select any item from listview and that item will set in text1 and text2 variable of PageActivity class respectively.

Question: How can I return result from tab activity to my PageActivity when user select any item from list view of any Tab.

1

There are 1 best solutions below

1
On

You can get the position from the ListView in the tabs, use that position to get corresponding data in the onClickListener.

Set this data in the TextViews. I am assuming ListViews' OnClickListener has reference to the TextViews