频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
简单实现一个android listview分类!
2013-12-17 11:31:45         来源:安卓小源的专栏  
收藏   我要投稿

最近在做一个关于招聘的APP,里面有选择城市一项,是用listview分类,有的人说两个listview嵌套,但是感觉太麻烦了,比较listview底层太复杂,有的人用expandablelistview,但是感觉效果一样不好,所以写了一个比较简单的,希望对大家有帮助!

1.首先看MainActivity类!

其实跟普通的代码一样,只是多了一个MyBean类!

package com.zhaoqingyuan.zhaoqingyuan;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView listView;
	private MyAdapter myAdapter;
	private Listlist=new ArrayList();//装数据的
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView=(ListView) findViewById(R.id.listView1);
		
		//装数据了
		//当前位置
		MyBean myBean=new MyBean("当前位置", "北京");
		list.add(myBean);
		//热门城市
		String[] hotCity={"全国","北京","上海","武汉","广州","天津","太原","南京","深圳","重庆","昆明","拉萨","石家庄","哈尔滨","贵阳"};
		for (int i = 0; i < hotCity.length; i++) {
			MyBean myBean2=new MyBean("热门城市", hotCity[i]);
			list.add(myBean2);
		}
		//热门城市
		
		String[] province={"河北省","河南省","云南省","辽宁省","黑龙江省","湖南省","安徽省","山东省","江苏省","浙江省",
				" 江西省","湖北省","甘肃省","山西省","内蒙古","陕西省","吉林省","福建省","贵州省","广东省",
				"青海省","西藏","四川省","宁夏回族","海南省","台湾省"};
		for (int i = 0; i < province.length; i++) {
			MyBean myBean3=new MyBean("按省份选择城市", province[i]);
			list.add(myBean3);
		}
		
		myAdapter=new MyAdapter(MainActivity.this, list);
		listView.setAdapter(myAdapter);
	}
}
2.这个是Bean类,里面有get和set方法!

package com.zhaoqingyuan.zhaoqingyuan;

public class MyBean {

	private String series;
	private String city;//可以替代省份名称
	public MyBean(String series,String city) {
		setSeries(series);
		setCity(city);
	}
	public String getSeries() {
		return series;
	}
	public void setSeries(String series) {
		this.series = series;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	
}
3.接下来就是我们在熟悉不过的适配器了!就是多了两行代码而已!

package com.zhaoqingyuan.zhaoqingyuan;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private Context context;
	private Listlist=new ArrayList();
	public MyAdapter(Context context,Listlist) {
		this.context=context;
		this.list=list;
	}
	
	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	Holder holder;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		holder=new Holder();
		if (convertView==null) {
			convertView=LayoutInflater.from(context).inflate(R.layout.item, null);
			holder.tv_series=(TextView) convertView.findViewById(R.id.tv_series);
			holder.tv_city=(TextView) convertView.findViewById(R.id.tv_city);
			convertView.setTag(holder);
		}else {
			holder=(Holder) convertView.getTag();
		}
		String series1=list.get(position).getSeries();
		String series2=position-1>=0?list.get(position-1).getSeries():"";
		if (!series1.equals(series2)) {
			holder.tv_series.setVisibility(View.VISIBLE);
			System.out.println(position);
			holder.tv_series.setText(series1);
			holder.tv_city.setText(list.get(position).getCity());
		}else {
			holder.tv_series.setVisibility(View.GONE);
			holder.tv_city.setText(list.get(position).getCity());
		}
		
		
		return convertView;
	}
	
	private class Holder{
		TextView tv_series,tv_city;
	}

}
4.来看下简单的XML代码



    
    





    

    

OK了,大功告成,让我们看一下效果吧!

\

\
n块エky源码点击此处下载




点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:android 子线程不用handler 控制UI线程中的组件
下一篇:Android Developers:跟踪移动
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站