精华内容
下载资源
问答
  • Android_TabHost+ListView组合使用,重写listview,可以全部显示listview
  • Android TabHost ListView初学者的实例代码
  • 1,我们要实现的功能是,动态获取商品分类,加载到tabhost中,并切换tabhost读取商品分类下的商品。且能够单独绑定listview中的imageview和其他控件。   这个功能在网上搜索后的问题有如下几点: 1,listview的...

    情况:最近ostocy-jshop这个开源项目,一直在进行pad版本的更新工作。今天把遇到的一些问题说说。

     

    1,我们要实现的功能是,动态获取商品分类,加载到tabhost中,并切换tabhost读取商品分类下的商品。且能够单独绑定listview中的imageview和其他控件。

     

    这个功能在网上搜索后的问题有如下几点:

    1,listview的onclick事件覆盖了imageview的事件。

    2,tabhost在extends 和 implements TabContentFactory的时候只会在第一次进入新tabhost的title才会读取。这不能符合我们的要求,我们希望每次切换tabhost都可以进行数据的读取。

    3,图片的压缩避免内存溢出。

     

    我们的代码解决了如上2个问题。

     

    主要的思路是:

    1,重写BaseAdapter对listview的空间进行单独绑定。

    2,在继承 extends TabActivity  implements TabContentFactory 并实现public View createTabContent(String tag) 方法后,同时给tabhost绑定onclick事件。但是这样可能会带来重复2次读取的操作,但是也就仅限在第一次,反复读取后并不会有重复的问题。

    3,对读取的图片进行压缩动作,并把原始图片进行调整大小处理。

     

    接下来贴出所有的代码

     

    主体

     

    package com.jshop.android.shop;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.AlertDialog;
    import android.app.TabActivity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TabHost;
    import android.widget.TabHost.OnTabChangeListener;
    import android.widget.TabHost.TabContentFactory;
    import android.widget.TextView;
    
    import com.jshop.android.action.JshopMGoodsListAction;
    import com.jshop.android.action.JshopMelectrocartAction;
    import com.jshop.android.action.JshopMgoodscategoryListAction;
    import com.jshop.android.holder.ElecartListViewHolder;
    import com.jshop.android.holder.GoodsListViewHolder;
    import com.jshop.android.index.R;
    import com.jshop.android.shop.JshopActivityGoodsList.JshopActivityGoodsListPageChangeListener;
    import com.jshop.android.sqlite.DBHelper;
    import com.jshop.android.util.Arith;
    import com.jshop.android.util.BaseTools;
    import com.jshop.android.widget.JshopListViewAdapter;
    import com.jshop.android.widget.JshopViewpagerAdapter;
    import com.jshop.android.widget.JshopViewpagerAdapter.JshopActivityGoodsListPageAdapter;
    
    public class JshopActivityNGoodsList extends TabActivity  implements TabContentFactory{
    	private final DBHelper dbhelper=new DBHelper(this);
    	private String[]tabTitle=null;
    	private GridView gv;
    	private ListView listViews;//used by goodslist
    	private ListView listViewForCart;//used by cartlist
    	private TextView totalmemberprice;//显示我的菜单总价
    	private Double total=0.0;
    	private List<Map<String,Object>>goodscategoryList=new ArrayList<Map<String,Object>>();//商品分类
    	private ArrayList<HashMap<String, Object>> electrocartgoodslists = new ArrayList<HashMap<String, Object>>();//elecart
    	private ArrayList<HashMap<String, Object>> goodslists = new ArrayList<HashMap<String, Object>>();//商品列表
    	private JshopMgoodscategoryListAction jmgclAction=new JshopMgoodscategoryListAction();
    	private JshopMGoodsListAction jmGoodslistAction=new JshopMGoodsListAction();
    	private JshopMelectrocartAction jmelecart=new JshopMelectrocartAction();
    	@SuppressWarnings("unchecked")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		super.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题窗口
    		super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏模式
    		super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
    		this.setContentView(R.layout.jshop_m_newgoodslist);
    		listViews=(ListView)this.findViewById(R.id.listViewfornewgoods);//商品列表的listview
    		listViewForCart=(ListView)this.findViewById(R.id.listViewforelecart);//我的菜单listview
    		setElecartListView();//调用读取我的菜单数据
    		
    		//读取商品分类缓存
    		Cursor c=dbhelper.query(DBHelper.GOODS_CATEGORY_TM_NAME);
    		goodscategoryList=jmgclAction.getGoodsCategoryListtoSQLite(c);
    		c.close();
    		if(goodscategoryList.isEmpty()){
    			goodscategoryList=jmgclAction.getGoodsCategoryList();
    			//缓存goodscategorylist
    			jmgclAction.setGoodsCategoryListtoSQLite(goodscategoryList, this.getApplicationContext());
    		}
    		setTabTitle(goodscategoryList);
    		if(tabTitle!=null){
    			final TabHost th = getTabHost();
    			for(int i = 0; i < tabTitle.length;i++){
    				LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.jshop_m_textfortabtitle,null);
    				((TextView) view.findViewById(R.id.tv_title)).setText(tabTitle[i]);
    				th.addTab(th.newTabSpec(tabTitle[i]).setIndicator(view).setContent(this));
    			}
    			th.getTabWidget().getChildAt(th.getCurrentTab()).setBackgroundColor(Color.parseColor("#ff58a300"));
    			th.setOnTabChangedListener(new OnTabChangeListener(){
    
    				@Override
    				public void onTabChanged(String tabId) {
    					// TODO Auto-generated method stub
    					SimpleListView(tabId);
    					
    					   // change tab background color to red
    			        for (int i = 0; i < th.getTabWidget().getChildCount(); i++) {
    			        	th.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ff003464"));
    			 
    			            /**
    			             * set height and width under TabActivity setting width has no
    			             * effect due to fill_parent layout parameter
    			             */
    			            //NOTE i cannot get this part work properly.
    			            //tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 30;
    			            //tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 30;
    			 
    			            View tempView= th.getTabWidget().getChildAt(i);
    			            /**
    			             * I kept all drawables in selector so that the we could get correct
    			             * drawablea applied to tabs as the selector pointed to has both the
    			             * tabs and the bottom tab-bar drawables referenced
    			             */
    			            //tempView.setBackgroundDrawable(res.getDrawable(R.drawable.somedrawable));
    			 
    			        }
    			 
    			        // o set different color for current selected tab to blue
    			        th.getTabWidget().getChildAt(th.getCurrentTab()).setBackgroundColor(Color.parseColor("#ff58a300"));
    
    					
    				}
    				
    			});
    		}
    	}
    	
    
    	/**
    	 * 读取我的菜单数据
    	 */
    	public void setElecartListView(){
    		electrocartgoodslists.clear();
    		//读取ele_cart缓存
    		Cursor ec=dbhelper.query(DBHelper.ELE_CART_TM_NAME);
    		electrocartgoodslists=jmelecart.getElecarttoSQLite(ec);
    		ec.close();
    		listViewForCart.setAdapter(new JshopMyElecartListViewAdapter(electrocartgoodslists,this.getApplicationContext()));
    		
    		setTotalMemberprice();
    	}
    	
    	/**
    	 * 设置计算我的菜单总价
    	 */
    	public void setTotalMemberprice(){
    		total=0.0;
    		if(!electrocartgoodslists.isEmpty()){
    			for(int i=0;i<electrocartgoodslists.size();i++){
    				total=Arith.add(total, Arith.mul(Double.parseDouble(electrocartgoodslists.get(i).get("memberprice").toString()), Double.parseDouble(electrocartgoodslists.get(i).get("needquantity").toString())));
    			}
    			totalmemberprice=(TextView) this.findViewById(R.id.totalmemberprice);
    			totalmemberprice.setText("¥"+total);
    		}else{
    			totalmemberprice=(TextView) this.findViewById(R.id.totalmemberprice);
    			totalmemberprice.setText("¥"+total);
    		}
    	}
    	
    	/**
    	 * 刷新我的菜单数据
    	 */
    //	public void rfreshElecartListView(){
    //		electrocartgoodslists.clear();
    //		//读取ele_cart缓存
    //		Cursor ec=dbhelper.query(DBHelper.ELE_CART_TM_NAME);
    //		electrocartgoodslists=jmelecart.getElecarttoSQLite(ec);
    //		ec.close();
    //		
    //	}
    	
    	
    	/**
    	 * 动态获取tabhost需要的title
    	 * @param goodscategoryList
    	 * @return
    	 */
    	@SuppressWarnings("unused")
    	private void setTabTitle(List<Map<String,Object>>goodscategoryList){
    		String [] t=new String[goodscategoryList.size()];
    		for(int i=0;i<goodscategoryList.size();i++){
    			t[i]=goodscategoryList.get(i).get("name").toString();
    		}
    		this.tabTitle=t;
    
    	}
    	
    	/**
    	 * 切换tabhost时调用的方法
    	 * @param tag
    	 * @return
    	 */
    	public View SimpleListView(String tag){
    		collectSqliteGoodsList(tag);
    		listViews.setAdapter(new JshopMyGoodsListViewAdapter(goodslists,this.getApplicationContext()));
    		return listViews;
    	}
    	
    	
    	@Override
    	public View createTabContent(String tag) {
    		View view = new View(this);
    			if(tabTitle!=null){
    				if(tabTitle[0].equals(tag)){
    					collectSqliteGoodsList(tag);
    				}else if (tabTitle[1].equals(tag)){
    					collectSqliteGoodsList(tag);
    				}else if (tabTitle[2].equals(tag)){
    					collectSqliteGoodsList(tag);
    				}else{
    					collectSqliteGoodsList(tag);
    				}
    				listViews.setAdapter(new JshopMyGoodsListViewAdapter(goodslists,this.getApplicationContext()));
    			}
    		return view;
    	}
    	
    
    	@SuppressWarnings("unused")
    	private  void collectSqliteGoodsList(String tag){
    		
    		Cursor c=dbhelper.queryByParamGoodsCategoryTName(DBHelper.GOODS_TM_NAME,tag);
    		try {
    			goodslists=jmGoodslistAction.getGoodsListSQLite(c);
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		c.close();
    	}
    
    	/**
    	 * 点击加入我的菜单
    	 * @param goodslists
    	 * @param arg2
    	 */
    	public void showConfirmAddtoCart(final ArrayList<HashMap<String, Object>> goodslists,final int arg2){
    
    		AlertDialog.Builder bulider=new AlertDialog.Builder(this);
    		bulider.setMessage("确定加入我的菜单吗?").setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				String goodsid=goodslists.get(arg2).get("goodsid").toString();
    				String goodsname=goodslists.get(arg2).get("goodsname").toString();
    				String memberprice=goodslists.get(arg2).get("memberprice").toString();
    				String pictureurl=goodslists.get(arg2).get("pictureurl").toString();
    				String needquantity="1";
    				jmelecart.setGoodsToElecartSQLite(goodsid,goodsname,memberprice,pictureurl,needquantity, getApplicationContext());
    				setElecartListView();
    				
    			}
    
    		}).setNegativeButton("取消", null);
    		AlertDialog alert=bulider.create();
    		alert.show();
    	}
    	
    	/**
    	 * 商品listview的适配器
    	 * @author "chenda"
    	 *
    	 */
    	public class JshopMyGoodsListViewAdapter extends BaseAdapter {
    		private ArrayList<HashMap<String, Object>> list;
    		private LayoutInflater myInflater;
    
    		public JshopMyGoodsListViewAdapter(
    				ArrayList<HashMap<String, Object>> list, Context context) {
    			this.list = list;
    			this.myInflater = LayoutInflater.from(context);
    		}
    
    		@Override
    		public int getCount() {
    			// TODO Auto-generated method stub
    			return list.size();
    		}
    
    		@Override
    		public Object getItem(int position) {
    			// TODO Auto-generated method stub
    			return list.get(position);
    		}
    
    		@Override
    		public long getItemId(int position) {
    			// TODO Auto-generated method stub
    			return position;
    		}
    
    		@Override
    		public View getView(final int position, View convertView,
    				ViewGroup parent) {
    			GoodsListViewHolder holder = null;
    			if (convertView == null) {
    				holder = new GoodsListViewHolder();
    				convertView = myInflater.inflate(
    						R.layout.jshop_m_listforcategory, null);
    				holder.setPictureurl((ImageView) convertView
    						.findViewById(R.id.pictureurl));
    				holder.setGoodsname((TextView) convertView
    						.findViewById(R.id.goodsname));
    				holder.setMemberprice((TextView) convertView
    						.findViewById(R.id.memberprice));
    				holder.setWeight((TextView) convertView
    						.findViewById(R.id.weight));
    				holder.setUnitname((TextView) convertView
    						.findViewById(R.id.unitname));
    				holder.setAddtomyelecartmenu((ImageView) convertView
    						.findViewById(R.id.addtomyelecartmenu));
    				holder.setDetail((TextView) convertView
    						.findViewById(R.id.detail));
    				convertView.setTag(holder);
    			} else {
    				holder = (GoodsListViewHolder) convertView.getTag();
    			}
    			holder.getPictureurl().setImageBitmap(
    					(Bitmap) list.get(position).get("pictureurl"));
    			holder.getGoodsname().setText(
    					list.get(position).get("goodsname").toString());
    			holder.getMemberprice().setText(
    					list.get(position).get("memberprice").toString());
    			holder.getWeight().setText(
    					list.get(position).get("weight").toString());
    			holder.getUnitname().setText(
    					list.get(position).get("unitname").toString());
    			holder.getDetail().setText(
    					list.get(position).get("detail").toString());
    			holder.getAddtomyelecartmenu().setOnClickListener(new OnClickListener() {
    					@Override
    					public void onClick(View v) {
    						showConfirmAddtoCart(list,
    								position);
    						
    					}
    				});
    			holder.getPictureurl().setOnClickListener(new OnClickListener(){
    
    				@Override
    				public void onClick(View v) {
    					
    				}
    				
    			});
    			return convertView;
    		}
    
    		@Override
    		public void notifyDataSetChanged() {
    			// TODO Auto-generated method stub
    			super.notifyDataSetChanged();
    		}
    		
    		
    		
    	}
    	
    	
    	/**
    	/**
    	 * 我的elecart的适配器
    	 * @author "chenda"
    	 *
    	 */
    	public class JshopMyElecartListViewAdapter extends BaseAdapter {
    		private final ArrayList<HashMap<String, Object>> list;
    		private LayoutInflater myInflater;
    
    		public JshopMyElecartListViewAdapter(
    				ArrayList<HashMap<String, Object>> list, Context context) {
    			this.list = list;
    			this.myInflater = LayoutInflater.from(context);
    		}
    
    		@Override
    		public int getCount() {
    			// TODO Auto-generated method stub
    			return list.size();
    		}
    
    		@Override
    		public Object getItem(int position) {
    			// TODO Auto-generated method stub
    			return list.get(position);
    		}
    
    		@Override
    		public long getItemId(int position) {
    			// TODO Auto-generated method stub
    			return position;
    		}
    
    		@Override
    		public View getView(final int position, View convertView,
    				final ViewGroup parent) {
    			ElecartListViewHolder holder = null;
    			if (convertView == null) {
    				holder = new ElecartListViewHolder();
    				convertView = myInflater.inflate(
    						R.layout.jshop_m_detaillistview, null);
    				holder.setGoodsname((TextView) convertView
    						.findViewById(R.id.goodsname));
    				holder.setMemberprice((TextView) convertView
    						.findViewById(R.id.memberprice));
    				holder.setNeedquantity((TextView) convertView.findViewById(R.id.needquantity));
    				holder.setPlus((ImageView) convertView.findViewById(R.id.plus));
    				holder.setMinus((ImageView) convertView.findViewById(R.id.minus));
    				convertView.setTag(holder);
    			} else {
    				holder = (ElecartListViewHolder) convertView.getTag();
    			}
    			holder.getGoodsname().setText(
    					list.get(position).get("goodsname").toString());
    			holder.getMemberprice().setText(
    					list.get(position).get("memberprice").toString());
    			holder.getNeedquantity().setText(
    					list.get(position).get("needquantity").toString());
    			holder.getPlus().setOnClickListener(
    					new OnClickListener() {
    						@Override
    						public void onClick(View v) {
    							jmelecart.plusorMinusElecart(list, position, "plus", v.getContext());
    							setElecartListView();
    							setTotalMemberprice();
    						}
    					});
    			holder.getMinus().setOnClickListener(
    					new OnClickListener() {
    						@Override
    						public void onClick(View v) {
    							jmelecart.plusorMinusElecart(list, position, "minus", v.getContext());
    							
    							setElecartListView();
    							setTotalMemberprice();
    						}
    					});
    			return convertView;
    		}
    
    		@Override
    		public void notifyDataSetChanged() {
    			// TODO Auto-generated method stub
    			super.notifyDataSetChanged();
    		}
    	}
    }
    

     

     

     

    	/**
    	 * 压缩图片
    	 * to change sd card pc to bmp type
    	 * @param pictureurl
    	 * @return
    	 * @throws IOException 
    	 */
    	public static Bitmap GetLocalOrNetBitmap(String url) throws IOException  
        {  
    		String sdcard=Environment.getExternalStorageDirectory().getPath();
    		BitmapFactory.Options options=new BitmapFactory.Options();
    		options.inSampleSize=6;
    		options.inTempStorage=new byte[5*1024];
    		Bitmap bitmap=BitmapFactory.decodeFile(sdcard+url,options);
    		
    		return bitmap;
        }  
    	/**
    	 * 不压缩图片
    	 * to change sd card pc to bmp type
    	 * @param pictureurl
    	 * @return
    	 * @throws IOException 
    	 */
    	public static Bitmap GetLocalOrNetBitmapNoZip(String url) throws IOException  
        {  
    		String sdcard=Environment.getExternalStorageDirectory().getPath();
    		BitmapFactory.Options options=new BitmapFactory.Options();
    		options.inTempStorage=new byte[5*1024];
    		Bitmap bitmap=BitmapFactory.decodeFile(sdcard+url,options);
    		
    		return bitmap;
        }  

     

     

    更多代码请关注 https://github.com/sdywcd/ostocy-jshop  中的jshop-android (项目会需要服务器端做支持,可能比较复杂。如果有问题欢迎通过站内信或者邮箱联系。)

    展开全文
  • 想在tabhost里面填入listview组件 求大神指教 谢谢啦 xml代码(activity_main.xml)   <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android...

    想在tabhost里面填入listview组件   求大神指教 谢谢啦

    xml代码(activity_main.xml)

     

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget 
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            >        
           </TabWidget>
          <FrameLayout 
              android:layout_width="match_parent"        
    		  android:layout_height="match_parent" 
    		  android:id="@android:id/tabcontent">
        <!-- 定义第一个标签页的内容 -->
        <LinearLayout
            android:id="@+id/tab01"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            >
           <TextView 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="活动1 - 2010/12/12"
    	android:textSize="11pt"
    	/>
          <TextView 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="活动2 - 2010/12/18"
    	android:textSize="11pt"
    	/>
        </LinearLayout>
        <!-- 定义第二个标签页的内容 -->
        <LinearLayout 
            android:id="@+id/tab02"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            >
            <ListView
            android:id="@+id/lv_very"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            >
        </ListView>
               
            </LinearLayout>        
            
         </FrameLayout>
        
    </TabHost>

     

    list_item.xml代码 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
         <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
       
    
        <TextView
            android:id="@+id/exercise_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            />
    
        <TextView
            android:id="@+id/exercise_collect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp" 
            />
    
        <TextView
            android:id="@+id/exercise_else"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp" 
            />
    
        <TextView
            android:id="@+id/exercise_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"        
            android:textSize="10sp" />  
                </LinearLayout>
    </LinearLayout>



    java代码

    package com.example.tabhost_listview_demo;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    
    
    import android.os.Bundle;
    import android.app.TabActivity;
    import android.view.LayoutInflater;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TabHost;
    
    public class MainActivity extends TabActivity {
    	private ListView lv_v;
    	private List list;
    	private SimpleAdapter adapter;
    		@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    
    		list = new ArrayList<Map<String, Object>>();
    		lv_v = (ListView)findViewById(R.id.lv_very);
    		Map<String, Object> map = new HashMap<String, Object>();
    		
    		map.put("item1_imageView1", R.drawable.ic_launcher);
    	       map.put("item1_exercise_name", "活动名称");
    	       map.put("item1_exercise_collect", "活动征集");
    	       map.put("item1_exercise_else", "其他");
    	       map.put("item1_exercise_details", "详情进入");
    		list.add(map);
    	    
    		adapter = new SimpleAdapter(this,
    				list,
    				R.id.lv_very,
    				new String [] {"item1_imageView1","item1_exercise_name",
    	    		   "item1_exercise_collect","item1_exercise_else","item1_exercise_details"},
    				new int [] {R.id.imageView1,R.id.exercise_name,
    				R.id.exercise_collect,R.id.exercise_else,R.id.exercise_details});
    //        		06-27 06:09:07.835: E/AndroidRuntime(922): java.lang.RuntimeException:
    //		       Unable to start activity ComponentInfo{com.example.tabhost_listview_demo/com.example.tabhost_listview_demo.MainActivity}: java.lang.NullPointerException
    
    		lv_v.setAdapter(adapter);
    		
    		TabHost tabHost = getTabHost();
    		//TabHost
    		LayoutInflater.from(this).inflate(R.layout.activity_main,
    			tabHost.getTabContentView(),true);
    		//第一选项卡
    		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("活动").
    				setContent(R.id.tab01));
    		
            //第二选项卡
    		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("竞选活动").
    				setContent(R.id.tab02));
    		setContentView(tabHost);
    }
    		
    		
    }
    


     

    展开全文
  • 点击ListView中所显示的目录(isDirectory),如果有下级目录就会在当前ListView显示下级目录,一直到没有目录或者到文件(File)为止. 问题:如何在TabHost中刷新ListView,而不是刷新整个Activity? 因为TabHost的默认...
  • ListView上拉加载下拉刷新升级版:滑动tab结合listView数据刷新
  • Android之TabHostListView结合应用

    千次阅读 2012-10-11 21:28:47
    一:创建每个选项卡ListView单个项内容布局: 代码:tab1_item.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34...

    实现效果:

    一:创建每个选项卡ListView单个项内容布局:

    代码:tab1_item.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?xmlversion="1.0"encoding="utf-8"?> 
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
                                  
        <ImageView
            android:id="@+id/image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="10dp"/> 
                                  
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
                                  
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                                  
                <TextView
                    android:id="@+id/text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/> 
                                  
                <TextView
                    android:id="@+id/text2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"/> 
            </LinearLayout
                                  
            <TextView
                android:id="@+id/text3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/> 
        </LinearLayout
                                  
    </LinearLayout>

    二:新建布局文件tab1.xml  存放ListView组件

    代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
                            
        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"/> 
                            
    </RelativeLayout>

    三:新建android activity

    Tab1Activity.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    publicclass Tab1Activity extends Activity { 
        privateListView listview; 
        @Override
        publicvoid onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.tab1); 
            List<Map<String, Object>> list =new ArrayList<Map<String, Object>>(); 
            list = getData(); 
            SimpleAdapter adapter =new SimpleAdapter(this, list, 
                    R.layout.tab1_item,new String[] { "image", "name","time"
                            "content"}, new int[] { R.id.image, R.id.text1, 
                            R.id.text2, R.id.text3 }); 
            listview=(ListView)this.findViewById(R.id.listview); 
            listview.setAdapter(adapter); 
        
                       
        privateList<Map<String, Object>> getData() { 
            List<Map<String, Object>> list =new ArrayList<Map<String, Object>>(); 
            Map<String, Object> map1 =new HashMap<String, Object>(); 
            map1.put("image", R.drawable.p1); 
            map1.put("name","香香"); 
            map1.put("time","1分钟前"); 
            map1.put("content","这是粉色金佛就是多几分感慨很多功课还是客观是个"); 
            list.add(map1); 
            Map<String, Object> map2 =new HashMap<String, Object>(); 
            map2.put("image", R.drawable.p2); 
            map2.put("name","永恒"); 
            map2.put("time","3分钟前"); 
            map2.put("content","今天天气真好,心情也舒畅!!!"); 
            list.add(map2); 
            Map<String, Object> map3 =new HashMap<String, Object>(); 
            map3.put("image", R.drawable.p3); 
            map3.put("name","海宝"); 
            map3.put("time","4分钟前"); 
            map3.put("content","能否从开始技术规范设计的感觉开始"); 
            list.add(map3); 
            Map<String, Object> map4 =new HashMap<String, Object>(); 
            map4.put("image", R.drawable.p4); 
            map4.put("name","樱木"); 
            map4.put("time","1小时前"); 
            map4.put("content","而他神色间若非他嗨哟对人体打发时间通融密瑞吉斯"); 
            list.add(map4); 
            Map<String, Object> map5 =new HashMap<String, Object>(); 
            map5.put("image", R.drawable.p5); 
            map5.put("name","潇潇"); 
            map5.put("time","1天前"); 
            map5.put("content","一直很高兴,天天开心"); 
            list.add(map5); 
            Map<String, Object> map6 =new HashMap<String, Object>(); 
            map6.put("image", R.drawable.p6); 
            map6.put("name","樱桃"); 
            map6.put("time","10分钟前"); 
            map6.put("content","sgaegeifero94eureg"); 
            list.add(map6); 
            Map<String, Object> map7 =new HashMap<String, Object>(); 
            map7.put("image", R.drawable.p7); 
            map7.put("name","莉莉"); 
            map7.put("time","2天前"); 
            map7.put("content","每天有什么事都说出来,这样感觉会很轻松,烦恼更少、幸福更多"); 
            list.add(map7); 
            returnlist; 
        
        @Override
        publicboolean onCreateOptionsMenu(Menu menu) { 
            getMenuInflater().inflate(R.menu.activity_main, menu); 
            returntrue
        
                       
                           
    }

    四:编写MainActivity.java   实现选项卡与ListView结合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    //继承TabActivity 
    publicclass MainActivity extends TabActivity { 
                
        @Override
        publicvoid onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            // 获取选项卡组 
            TabHost host = getTabHost(); 
            /* 
             * 创建Tab对象 创建意图,tab选项卡与另一个activity连接, 
             * 可以每一个选项对应一个内容,定义一个需要新建一个意图Activity类,设置内容为该意图就可实现 
             */
            Intent intent =new Intent(); 
            intent.setClass(this, Tab1Activity.class); 
            Resources r = getResources(); 
            TabHost.TabSpectab1 = host.newTabSpec("tab1"
                    .setIndicator("选项1", r.getDrawable(R.drawable.p5)) 
                    .setContent(intent); 
            TabHost.TabSpec tab2 = host.newTabSpec("tab2"
                    .setIndicator("选项2", r.getDrawable(R.drawable.p6)) 
                    .setContent(intent); 
            TabHost.TabSpec tab3 = host.newTabSpec("tab3"
                    .setIndicator("选项3", r.getDrawable(R.drawable.p7)) 
                    .setContent(intent); 
            // tab与选项卡组绑定 
            host.addTab(tab1); 
            host.addTab(tab2); 
            host.addTab(tab3); 
        
                
        @Override
        publicboolean onCreateOptionsMenu(Menu menu) { 
            getMenuInflater().inflate(R.menu.activity_main, menu); 
            returntrue
        
                
    }
    展开全文
  • 本程序 涉及到了listview的多选 删除 添加。以及tabhost的使用。代码可能有点混乱, 这也是我结合网上资料做的,将就看吧。效果类似ucweb的历史书签管理。当然很难看啊 效果我感觉就是那个意思,比较简陋。见谅了 ...

    本程序 涉及到了listview的多选 删除 添加。以及tabhost的使用。代码可能有点混乱, 这也是我结合网上资料做的,将就看吧。效果类似ucweb的历史书签管理。当然很难看啊 效果我感觉就是那个意思,比较简陋。见谅了

     

    下看下面效果图

    默认显示基本设置

    点击管理后 的效果

     

     点击添加的效果

     

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    <!-- RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    -->
    <TabHost android:id="@id/android:tabhost"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_above="@+id/gridview"
        >
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
              <TabWidget android:id="@id/android:tabs"
                  android:layout_height="wrap_content"
                  android:layout_width="fill_parent"
                  android:gravity="left"
                  
                  />
              <FrameLayout android:id="@id/android:tabcontent"
                  android:layout_height="fill_parent"
                  android:layout_width="fill_parent"
                  
                  > 
                  <LinearLayout android:id="@+id/cb"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    >
                    <LinearLayout 
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        
                    >
                    <CheckBox android:id="@+id/checkbox01" 
                            android:layout_width="fill_parent" 
                             android:layout_height="wrap_content" 
                            android:text=" 启用 自定义配置"
                          />
                     <CheckBox android:id="@+id/checkbox02" 
                            android:layout_width="fill_parent" 
                            android:layout_height="wrap_content" 
                            android:text=" 启用默认配置"
                          />
                    </LinearLayout>
                    <GridView android:id="@+id/gridview"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                     android:background="@drawable/button"
                        />
                        <!--  -->
                    
                   </LinearLayout>
                <LinearLayout android:id="@+id/list"    
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:orientation="vertical"
                      >
                      <ListView android:id="@+id/listview"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:cacheColorHint="#00000000"
                          android:layout_weight="1" 
                          >
                          <!--android:background="@drawable/listviewbackground"  -->
                      </ListView> 
                      <GridView android:id="@+id/gridview01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/button"
                    />  
                  </LinearLayout>
              </FrameLayout>
        </LinearLayout>
    </TabHost>
    </LinearLayout>

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
      
     <ImageView 
         android:id="@+id/imgview"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="5dp"
        />
     
      <TextView 
        android:id="@+id/link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="3dp"
           android:gravity="center_vertical"
      />
      
    </LinearLayout>

     

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, GreenActivity!</string>
        <string name="app_name">green</string>
        <drawable name="gridviewbackground">#0077AA</drawable>
        <drawable name="listviewbackground">#FFFFFF</drawable> 
        <drawable name="black">#000000</drawable>
        <drawable name="bg">#E6ECF6</drawable>
        <drawable name="fc">#99CCFF</drawable>
        <drawable name="gd">#577FB5</drawable>
    </resources>

     

     

    GreenActivity.java
    package com.android.green;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map; 
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.app.Dialog;
    import android.app.TabActivity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.opengl.Visibility;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Adapter;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.EditText;
    import android.widget.GridView;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TabHost;
    import android.widget.TabHost.OnTabChangeListener;
    import android.widget.TabWidget;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class GreenActivity extends TabActivity {
        /** Called when the activity is first created. */
        private GridView configGrid, managerGrid;
        private String configData[] = {"应用","退出"};
        private String managerData[] ={"添加","删除","退出"};
        private ListView listview = null;
        private SimpleAdapter adapter;
        private List list;
        
        private Map<String, Object> listTemp;
        private EditText et ;
        private CheckBox whiteCb, defWhiteCb;
        private int index = 0;
        private AlertDialog addDlg, exitDlg, removeDlg;
        /*-- configGrid底部菜单选项下标--*/
        private final int CONFIG_ITEM_APPLY = 0;// 应用
        private final int CONFIG_ITEM_EXIT = 1;// 退出
        /*-- managerGrid底部菜单选项下标--*/
        private final int MANAGE_ITEM_ADD = 0; // 添加
        private final int MANAGE_ITEM_REMOVE = 1; // 删除
        private final int MANAGE_ITEM_EXIT = 2; // 退出
         
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
            whiteCb = (CheckBox)findViewById(R.id.checkbox01);
            defWhiteCb = (CheckBox)findViewById(R.id.checkbox02);
            
            whiteCb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    if (whiteCb.isChecked())
                    {
                        System.out.println("white is checked");
                    }
                }
            });
            defWhiteCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                 
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                     
                    if(defWhiteCb.isChecked())
                    System.out.println("default white is checked");
                }
            });
            // 对话框添加
            et = new EditText(this);
            et.setSingleLine(true);
            addDlg = new AlertDialog.Builder(GreenActivity.this).setTitle("添加页面").setView(et).
            setPositiveButton("确定",new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    
                     addItem(et.getText().toString());
                }
                 
            }).setNegativeButton("取消", null).create() ;
      
            //退出确认对话框
            exitDlg = new AlertDialog.Builder(this).setTitle("友情提醒").setMessage("确定要退出吗?").
            setPositiveButton("确定",new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    finish();
                }
            }).setNegativeButton("取消", null).create();
            
            // 删除确认对话框
            removeDlg = new AlertDialog.Builder(this).setTitle("删除提示").setMessage("确定删除选中的内容吗?").
            setPositiveButton("确定", new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    removeS(list); 
                }
            }).setNegativeButton("取消", null).create();
            
            //TabHost的相关设置
            int width =45;  
            int height =50;
            final TabHost tabhost = getTabHost();
            tabhost.addTab(tabhost.newTabSpec("checkbox").setIndicator("基本配置").setContent(R.id.cb));
            tabhost.addTab(tabhost.newTabSpec("listview01").setIndicator("管理").setContent(R.id.list));
            final TabWidget tabwidget = tabhost.getTabWidget();
           
            for (int i = 0; i < tabwidget.getChildCount(); i++)
            {
                //设置页签高度和页签内字体属性
                TextView tv = (TextView)tabwidget.getChildAt(i).findViewById(android.R.id.title);
                tabwidget.getChildAt(i).getLayoutParams().height = height;  
                tabwidget.getChildAt(i).getLayoutParams().width = width; 
           //     tabwidget.setBackgroundResource(R.drawable.toolbar);
                tv.setTextSize(15);
                tv.setTextColor(Color.BLACK);
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
            }
           //配置页gridview的设置
            configGrid = (GridView)findViewById(R.id.gridview);
            configGrid.setNumColumns(configData.length);
            GridAdapter gridAdapter = new GridAdapter(this,Arrays.asList(configData));
            configGrid.setAdapter(gridAdapter);
          //白名单页gridview的设置
            managerGrid = (GridView)findViewById(R.id.gridview01);
            managerGrid.setNumColumns(managerData.length);
            GridAdapter grid01Adapter = new GridAdapter(this,Arrays.asList(managerData));
             managerGrid.setAdapter(grid01Adapter);
            
             listview = (ListView)findViewById(R.id.listview);
             adapter = new SimpleAdapter(this, getData(),
                      R.layout.listview, new String[]{"link","img"},new int[]{R.id.link, R.id.imgview});                  
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(new OnItemClickListener() {
    
                // 捕捉点击 对图片进行变换
                 
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
                { 
                    Map <String, Object>m1 = (Map <String, Object>)list.get(arg2);
                    if (m1.get("img").equals( 2130837509))         //2130837509为图片select.jpg在R中的编号
                    {
                        ChangeImg(arg2,true);
                    }
                    else
                    {
                        ChangeImg(arg2,false);
                    }
                }
                
            });
            
            configGrid.setOnItemClickListener( new OnItemClickListener(){
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                {
                    switch(arg2)
                    {
                    case CONFIG_ITEM_APPLY:        // 发送白名单 默认白名单的配置 接收配置
                        System.out.println("hello world");
                        break;
                    case CONFIG_ITEM_EXIT:  
                        finish();
                        break;
                    default:
                    }
                }                  
             });
             
            //白名单管理页面的监听器
             managerGrid.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
                {
                    switch(arg2)
                    {
                    case MANAGE_ITEM_ADD:     // 弹出一个页面用来添加白名单 发送消息
                            addDlg.show();      // 从dialog中接受输入的内容
                        break;
                    case MANAGE_ITEM_REMOVE://删除选中的listview项  发送消息
                        removeDlg.show();
                        break;
                    case MANAGE_ITEM_EXIT:    // 退出
                        exitDlg.show(); 
                        break;
                    default:
                    }
                }             
             });
             
        }
        
        //变换图像(选中未选中)
        private void ChangeImg(int selectedItem, boolean b)
        {
            SimpleAdapter la = adapter;
            HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(selectedItem);
            if (b)
                map.put("img", R.drawable.radio_on);
            else 
                map.put("img", R.drawable.radio_off);
            la.notifyDataSetChanged();
        }
        
        public class GridAdapter extends BaseAdapter{
            Context context;
            List list;
            public GridAdapter(Context c,List datalist){
                this.context = c;
                list = datalist;
            }
         
            public GridAdapter(OnTabChangeListener onTabChangeListener,
                    List<String> asList) {
                // TODO Auto-generated constructor stub
            }
            public int getCount() {
                // TODO Auto-generated method stub
                return list.size();
            }
    
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup arg2) {
                TextView tv;
                if(convertView == null){
                    tv = new TextView(context);
                }else{
                    tv = (TextView)convertView;
                }
                tv.setLayoutParams( new GridView.LayoutParams(110,48));
                tv.setTextColor(Color.WHITE);
                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(20);
                tv.setText(list.get(position).toString());
         
                return tv;
            }
            
        }
        /**
         * 初始化list
         * @return
         */
        private List getData(){
            list = new ArrayList();
            Map<String,Object> map = new HashMap<String,Object>();
            Map<String,Object> map2 = new HashMap<String,Object>();
            Map<String,Object> map3 = new HashMap<String,Object>();
            Map<String,Object> map4 = new HashMap<String,Object>();
            Map<String,Object> map5 = new HashMap<String,Object>();
            Map<String,Object> map6 = new HashMap<String,Object>();
            Map<String,Object> map7 = new HashMap<String,Object>();
            Map<String,Object> map8 = new HashMap<String,Object>();
            Map<String,Object> map9 = new HashMap<String,Object>();
             
            map.put("img", R.drawable.radio_off);
            map.put("link", "http://3g.uc.cn/portal/index.html");
            map2.put("img", R.drawable.radio_off);
            map2.put("link", "http://mv.ucweb.com/e?id=118");
            map3.put("img", R.drawable.radio_off);
            map3.put("link", " http://mv.ucweb.com/t?id=bookmarks-rss-android");
            map4.put("img", R.drawable.radio_off);
            map4.put("link", "http://mv.ucweb.com/t?id=bookmarks-rss-android");
            map5.put("img", R.drawable.radio_off);
            map5.put("link", "http://mv.ucweb.com/t?id=bookmarks-rss-android");
            map6.put("img", R.drawable.radio_off);
            map6.put("link", "http://mv.ucweb.com/t?id=bookmarks-rss-android");
            map7.put("img", R.drawable.radio_off);
            map7.put("link", "http://mv.ucweb.com/t?id=bookmarks-rss-android");
             map8.put("img", R.drawable.radio_off);
            map8.put("link", "http://mv.ucweb.com/t?id=bookmarks-rss-android");
             map9.put("img", R.drawable.radio_off);
            map9.put("link", "http://mv.ucweb.com/e?id=118"); 
            
            list.add(map);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
            list.add(map6);
            list.add(map7);
            list.add(map8);
            list.add(map9);
            return list;
        }
        
        //删除listview被选中的item
         public void removeS(List list)
        {
              ArrayList<Integer>  indexTemp = new ArrayList<Integer>();
              int size = list.size();
              int temp;
              System.out.println("list.size = "+size);
              for (int i = 0; i < size; i++)
              {
                   listTemp = (Map<String, Object>)list.get(i);
                   if (listTemp.get("img").equals(2130837510))
                   {
                       indexTemp.add(i);
                   }
              }
              if (0 != indexTemp.size())
              {
                  for (int i=  0; i<indexTemp.size(); i++)
                  {
                      if (i == 0)
                      {
                          temp =  (Integer)indexTemp.get(i);
                      }
                      else
                      {
                          temp = (Integer)indexTemp.get(i)-i;
                     
                      }
                      list.remove(temp);
                      adapter.notifyDataSetChanged(); 
                  }
              }      
        }
         
         //为listview 添加item
         public void addItem(String linkString)
         {
             HashMap<String, Object> map = new HashMap<String, Object>();
             map.put("img", R.drawable.radio_off);
             map.put("link",linkString);
             list.add(map);
             adapter.notifyDataSetChanged();
         }
         
    }

     

     

    转载于:https://www.cnblogs.com/firecode/archive/2012/09/06/2673524.html

    展开全文
  • 代码就转载了网友的部分,发现关键之处在红色部分:即不能用setContentView(R.layout.activity_main); 而要用LayoutInflater.from(this).inflate(R.layout....现在说一下如何把TTabHost + ListView 二者结合...
  • android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/toplayout" android:layout_width="fill_parent" ...
  • 3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.html Android 实现tab视图有2种方法,一种是在布局页面中定义标签,另一种就是继承tabactivity.但是...
  • 3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.html Android实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承...
  • 3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.html Android 实现tab视图有2种方法,一种是在布局页面中定义标签,另一种就是继承tabactivity.但是...
  • android实现tabhost并且tab内容是listview,没有view可以左右滑动。
  • 3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.html Android实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承...
  • TabHost组件和ListView组件的使用,大综合
  • TabHost,ViewPager,ListView综合demo

    热门讨论 2013-04-23 13:43:45
    一个关于 TabHost ViewPager ListView的综合demo,学习记录,很简单,请不要喷我
  • TabHost组件和ListView组件的结合使用

    千次阅读 2012-12-10 21:05:32
    在前边都分别介绍了TabHost组件和ListView组件的使用,现在说一下如何把二者结合起来使用......当点击上面的tabHost选项卡会出现以ListView组件显示的心情博客...下面就说一下它的具体实现。 阶段一:主界面的布局,具
  • TabHost中填充自定义ListView

    热门讨论 2012-03-22 11:37:32
    TabHost中填充自定义ListView
  • 如题,谢谢大家了!布局中如何让tabhost显示listview之上,而不因为listview的内容太多而不显示tabhost?
  • 加个listview.setItemsCanFocus(true)就可以了
  • 里面有关于listview tabhost 的各种使用 还有 Android ListView从网络获取图片及文字显示
  • TabHost+三种Listview

    2018-04-06 16:09:57
    ListView listView=(ListView)findViewById(R.id.listView1); names=getResources().getStringArray(R.array.country); MyAdapter adapter=new MyAdapter(images, names, this); listView.setAdapter...
  • ListView+Tabhost小实例

    2012-10-30 23:23:09
    ListView+Tabhost小实例,内附讲解
  • TabHost,ViewPager,ListView综合demo

    千次阅读 2013-04-23 13:44:51
    维护的项目里面这三个东西就是综合应用的,一直也没有自己动手写过任何东西,这次动手写一下。...首先关于非继承TabActivity 的TabHost 的创建 可以参考下面这个文章 http://blog.csdn.net/chenzheng_jav
  • Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabHost是标签控件类的核心类,也是标签的集合...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,143
精华内容 1,257
关键字:

tabhost显示listview