在TabHost中重新加载选项卡内容

时间:2012-08-31 20:37:14

标签: android android-tabhost reload

我有一个带有2个标签的Tabview,在我的TabActivity中,有一个按钮。单击该按钮时,我希望将数据发送到当前选项卡并将数据显示到当前选项卡。当我提供这些数据时,数据可用,但是当我将此数据设置为textview时,我的textview不会改变。如何从tabhost活动刷新选项卡内容? (抱歉我的英语不好:P)这是我的代码:

public class MainActivity extends TabActivity {

TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tabHost = getTabHost();
    Bundle b = new Bundle();
    b.putInt("category_id", 10);

    Intent tab1Intent = new Intent(this, Tab1Activity.class).putExtras(b);
    TabSpec tab1Spec = tabHost
            .newTabSpec("Tab1")
            .setIndicator("Tab1",
                    getResources().getDrawable(R.drawable.credit_tab_icon))
            .setContent(tab1Intent);
    tabHost.addTab(tab1Spec);

    Intent tab2Intent = new Intent(this, Tab2Activity.class);
    TabSpec tab2Spec = tabHost
            .newTabSpec("Tab2")
            .setIndicator("Tab2",
                    getResources().getDrawable(R.drawable.credit_tab_icon))
            .setContent(tab2Intent);
    tabHost.addTab(tab2Spec);

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 80;
    }
    tabHost.getTabWidget().setCurrentTab(0);

    Button ok   =   (Button) findViewById(R.id.ok);
    ok.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("category_id",103);
            LocalActivityManager manager            =   getLocalActivityManager();
            String currentTag                       =   tabHost.getCurrentTabTag();
            Class<? extends Activity> currentClass  =   manager.getCurrentActivity().getClass();
            manager.destroyActivity(currentTag, true);
            manager.startActivity(currentTag, new Intent(MainActivity.this, currentClass).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(bundle));
        }
    });
}

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

}

这是我的Tab1Activity:

public class Tab1Activity extends Activity {
public static Tab1Activity s_childActivity;

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

public void onResume() {
    super.onResume();
    Bundle extras = this.getIntent().getExtras();
    int get_category_id = extras.getInt("category_id");
    Toast.makeText(this, "category_id = " + get_category_id,
            Toast.LENGTH_SHORT).show();


    TextView textView1  =   (TextView)this.findViewById(R.id.textView1);
    textView1.setText("Berubah"+get_category_id);//==> Textview not change with category_id
}

}

2 个答案:

答案 0 :(得分:1)

试试这段代码。

getTabHost().invalidate()

它应该再次重绘标签,因此它们的内容会更新。

答案 1 :(得分:0)

您可以获取当前标签并在按钮上刷新TextView,如下所示:

if (getCurrentActivity() instanceof Tab1Activity ) {
                                    ((TextView)((ProductListTab) getCurrentActivity())
                                                     .findViewById(R.id.textView1)).setText("103");
                                }