tabhost 多页公用一个listview

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为你提供一个简单的示例代码,演示如何在Android Studio中动态添加项目的TabHost。 1. 首先,在XML布局文件中添加TabHost组件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </TabHost> ``` 2. 在Java代码中找到对应的TabHost组件并初始化: ```java TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 创建一个新的TabSpec对象,用于描述每个标签页: ```java TabHost.TabSpec spec; spec = tabHost.newTabSpec("tag1"); spec.setIndicator("Tab1"); spec.setContent(R.id.tab1); tabHost.addTab(spec); ``` 4. 重复步骤3,以添加其他标签页: ```java spec = tabHost.newTabSpec("tag2"); spec.setIndicator("Tab2"); spec.setContent(R.id.tab2); tabHost.addTab(spec); spec = tabHost.newTabSpec("tag3"); spec.setIndicator("Tab3"); spec.setContent(R.id.tab3); tabHost.addTab(spec); ``` 5. 最后,你可以在代码中动态添加标签页: ```java TabHost.TabSpec spec = tabHost.newTabSpec("tag4"); spec.setIndicator("Tab4"); spec.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { TextView textView = new TextView(MainActivity.this); textView.setText("This is Tab 4"); return textView; } }); tabHost.addTab(spec); ``` 这个示例演示了如何在Android Studio中动态添加项目的TabHost。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值