Lauren
Lauren

Reputation: 11

Changing Activities in Android - List inside Tab

Hey there, I've been searching stack overflow for the same question as this but I cannot seem to make it work and I am desperately hoping someone can help!

I have got three tabs, each with their own activity. In the first tab I have implemented a list within the tab and it has some values which display no problem. The other two tabs have table layouts.

Here's the issue: I need to switch to the second tab when any of the list items are clicked.

How do I do this? I have read several pages on the internet about registering intent, but can't seem to make anything work with a tabHost implementing a listView.

Code below!

Many thanks.

public class AlertsActivity extends TabActivity implements OnTabChangeListener, OnClickListener { private static final String LIST1_TAB_TAG = "Saved Alerts"; private ListView listView1; private TabHost tabHost;

@Override 
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.settings:
        startActivity(new Intent(this, Prefs.class));
        return true;
    }
return false;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    tabHost = getTabHost();
    tabHost.setOnTabChangedListener(this);

    listView1 = (ListView) findViewById(R.id.list);

     List<String> listStrings1 = new ArrayList<String>();
    listStrings1.add("Item 1");
    listStrings1.add("Item 2");
    listStrings1.add("Item 3");
    listStrings1.add("Item 4");
    listStrings1.add("Item 5");
    listStrings1.add("Item 6");
    listStrings1.add("Item 7");
    listStrings1.add("Item 8");
    listStrings1.add("Item 9");
    listStrings1.add("Item 10");

    listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listStrings1));

    tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
        public View createTabContent(String arg0) {
         return listView1;
        }
    }));

}

public void onTabChanged(String tabName) {
    if(tabName.equals(LIST1_TAB_TAG)) {

    }
}

public void onClick(View src) {

}

}

Upvotes: 1

Views: 715

Answers (1)

Jayy
Jayy

Reputation: 14728

In your listener, (within your TabActivity) just do:

getTabHost().setCurrentTab(index);

TabHost documentation for setCurrentTab(int)

TabActivity documentation for getTabHost()

Upvotes: 1

Related Questions