문제

i have tabview with 4 tabs and i am using TabHost to display tabs my application. every tab is filled by another class extends from ListActivity and here is the code

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
}

and this the code at the other intent

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);
}}

at the Listview there is list of items ,, my Point is how to set the Onclick to open another "ListActivity" in the same Tab ?!!

도움이 되었습니까?

해결책

In your Category class, add an onClickListener like so:

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 would look like the category class with the ListAdapter but with the information that was passed through the intent:

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);
}}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top