4

我有一个带有 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
}

}

4

2 回答 2

1

试试这个代码。

getTabHost().invalidate()

它应该再次重绘选项卡,因此它们的内容会得到更新。

于 2012-08-31T20:42:12.020 回答
0

You can get the current tab and refresh TextView in your button onClick like this:

if (getCurrentActivity() instanceof Tab1Activity ) {
                                    ((TextView)((ProductListTab) getCurrentActivity())
                                                     .findViewById(R.id.textView1)).setText("103");
                                }
于 2014-08-25T06:41:04.963 回答