TabHost.setCurrentTab() 方法无效
TabHost.setCurrentTab() method is not working
I am using TabHost and I have to change the default Tabselected
according to the users selected option in previous class but it is not working.
这是我的代码。
public class FirstActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
String getselectevalue ="0";
private static final String TAB_1_TAG = "tab_1";
private static final String TAB_2_TAG = "tab_2";
private static final String TAB_3_TAG = "tab_3";
private static final String TAB_4_TAG = "tab_4";
private static final String TAB_5_TAG = "tab_5";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
InitView();
getActionBar().setDisplayHomeAsUpEnabled(true);
// mTabHost = new FragmentTabHost(this);
// mTabHost.setup(this, getSupportFragmentManager(),
// R.id.menu_settings);
try{
getselectevalue = getIntent().getExtras().getString("selectedVlaue");
}catch(Exception e){
}
Log.d("got the value here", getselectevalue);
}
private void InitView() {
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_1_TAG),
R.drawable.tab_indicator_gen,"Post",R.drawable.post_unselected),Fragment1.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_2_TAG),
R.drawable.tab_indicator_gen,"Profile",R.drawable.profile_unselect),Fragment2.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_3_TAG),
R.drawable.tab_indicator_gen,"Chat",R.drawable.chat_unselected),Fragment3.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_4_TAG),
R.drawable.tab_indicator_gen,"Event",R.drawable.event_unselcted),Fragment3.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_5_TAG),
R.drawable.tab_indicator_gen,"Fragment",R.drawable.shop_unselected),Fragment3.class,null);
if(getselectevalue.equalsIgnoreCase("0")){
mTabHost.setCurrentTab(0);
}else{
mTabHost.setCurrentTab(1);
}
}
private TabSpec setIndicator(Context ctx, TabSpec spec,
int resid, String string, int genresIcon) {
View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
v.setBackgroundResource(resid);
TextView tv = (TextView)v.findViewById(R.id.txt_tabtxt);
ImageView img = (ImageView)v.findViewById(R.id.img_tabtxt);
tv.setText(string);
img.setBackgroundResource(genresIcon);
return spec.setIndicator(v);
}
Please have a look and let me know what mistake I am doing here.
首先在catch block
之后调用InitView();
方法。
try
{
getselectevalue = getIntent().getExtras().getString("selectedVlaue");
}catch(Exception e)
{
}
InitView();
I am using TabHost and I have to change the default Tabselected according to the users selected option in previous class but it is not working.
这是我的代码。
public class FirstActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
String getselectevalue ="0";
private static final String TAB_1_TAG = "tab_1";
private static final String TAB_2_TAG = "tab_2";
private static final String TAB_3_TAG = "tab_3";
private static final String TAB_4_TAG = "tab_4";
private static final String TAB_5_TAG = "tab_5";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
InitView();
getActionBar().setDisplayHomeAsUpEnabled(true);
// mTabHost = new FragmentTabHost(this);
// mTabHost.setup(this, getSupportFragmentManager(),
// R.id.menu_settings);
try{
getselectevalue = getIntent().getExtras().getString("selectedVlaue");
}catch(Exception e){
}
Log.d("got the value here", getselectevalue);
}
private void InitView() {
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_1_TAG),
R.drawable.tab_indicator_gen,"Post",R.drawable.post_unselected),Fragment1.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_2_TAG),
R.drawable.tab_indicator_gen,"Profile",R.drawable.profile_unselect),Fragment2.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_3_TAG),
R.drawable.tab_indicator_gen,"Chat",R.drawable.chat_unselected),Fragment3.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_4_TAG),
R.drawable.tab_indicator_gen,"Event",R.drawable.event_unselcted),Fragment3.class,null);
mTabHost.addTab(setIndicator(FirstActivity.this,mTabHost.newTabSpec(TAB_5_TAG),
R.drawable.tab_indicator_gen,"Fragment",R.drawable.shop_unselected),Fragment3.class,null);
if(getselectevalue.equalsIgnoreCase("0")){
mTabHost.setCurrentTab(0);
}else{
mTabHost.setCurrentTab(1);
}
}
private TabSpec setIndicator(Context ctx, TabSpec spec,
int resid, String string, int genresIcon) {
View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
v.setBackgroundResource(resid);
TextView tv = (TextView)v.findViewById(R.id.txt_tabtxt);
ImageView img = (ImageView)v.findViewById(R.id.img_tabtxt);
tv.setText(string);
img.setBackgroundResource(genresIcon);
return spec.setIndicator(v);
}
Please have a look and let me know what mistake I am doing here.
首先在catch block
之后调用InitView();
方法。
try
{
getselectevalue = getIntent().getExtras().getString("selectedVlaue");
}catch(Exception e)
{
}
InitView();