使用 intent
for(MenuCategory category:categories){
TabSpec tabSpec = tabhost.newTabSpec(Integer.toString(category.getMenuId()));
tabSpec.setIndicator(category.getMenuName());
Intent intent=new Intent();
intent.putExtra("cid", category.getMenuId());
intent.setClass(this, MyListActivity.class);
tabSpec.setContent(intent);
tabhost.addTab(tabSpec);
}
mylistactivity 代码
package com.kaiff.dapangandxiaoff;
import java.io.IOException;
import java.util.List;
import com.kaiff.db.DBManager;
import com.kaiff.entity.Food;
import com.kaiff.entity.FoodAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MyListActivity extends Activity {
private DBManager mgr;
public static ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmain);
listView=(ListView)findViewById(R.id.listView);
Intent intent=getIntent();
int cid=intent.getIntExtra("cid", 1);
try {
mgr=new DBManager(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Food> foods=mgr.queryFoodsByCategoryId(cid);
FoodAdapter adapter = new FoodAdapter(this, foods, R.layout.listitem);
listView.setAdapter(adapter);
}
}