我有4个标签的tabview,我正在使用TabHost显示我的应用程序标签。 每个选项卡都由ListActivity扩展的另一个类填充,这里是代码
public class TabbedActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_layout);
TabHost tabHost = getTabHost();
// Tab for Catalog
TabSpec catalogspec = tabHost.newTabSpec("Catalog");
catalogspec.setIndicator("Complete Catalog Fall 2012", getResources().getDrawable(R.drawable.ic_catalog));
Intent catalogIntent = new Intent(this, Category.class);
catalogspec.setContent(catalogIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(catalogspec); // Adding catalog tab
}
这是另一个意图的代码
public class Category extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_data);
Categories = new ArrayList<String>();
fillListCategories();
myListItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Categories);
this.setListAdapter(adapter);
}}
在Listview上有项目列表,我的Point是如何设置Onclick在同一个标签中打开另一个“ListActivity”?!!
答案 0 :(得分:1)
在Category类中,添加一个onClickListener,如下所示:
public class Category extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_data);
Categories = new ArrayList<String>();
fillListCategories();
myListItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Categories);
this.setListAdapter(adapter);
this.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View item,
int position, long id) {
mSelectedCategory = (String)parent.getItemAtPosition(position);
Intent intent = new Intent(getBaseContext(), ScreenTwo.class);
intent.putExtra("name", mSelectedCategory.(WHATEVER INFO YOU NEED ABOUT THE CATEGORY);
startActivity(intent);
}
});
}}
ScreenTwo.java看起来像是带有ListAdapter的类别类,但是带有通过意图传递的信息:
public class ScreenTwo extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_data);
Bundle b = getIntent().getExtras();
String Name = b.getString("name");
Categories = new ArrayList<String>();
fillListCategories();
myListItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Categories);
this.setListAdapter(adapter);
}}