我有带有动态创建选项卡的 tabHost 小部件。在每个新创建的选项卡中,都有一些视图(textView、listView 等)的相对布局。
我的问题是:如何访问这些视图以及如何在其中添加新内容?我尝试设置 ID 或标签,但我没有工作。
public class Main extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
spec.setContent(R.id.form);
spec.setIndicator("Search-form");
tabs.addTab(spec);
Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(Main.this);
// Defining the RelativeLayout layout parameters.
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView t = new TextView(Main.this);
// t.setId(12); //This dosen't work
t.setText("Searching in progress. Please wait...");
ListView resultsList = new ListView(Main.this);
//Here: array adapter etc
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(t);
relativeLayout.addView(resultsList);
return relativeLayout;
}
});
spec.setIndicator("Tab with results");
tabs.addTab(spec);
new GetData(Main.this).execute();
}
});
}
public void appendResultsToViews(String result)
{
//Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
}
谢谢您的帮助。