0

我有 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 有项目列表,我的观点是如何设置 Onclick 在同一个选项卡中打开另一个“ListActivity”?!

4

1 回答 1

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);
}}
于 2012-09-06T08:43:41.663 回答