码迷,mamicode.com
首页 > 移动开发 > 详细

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色

时间:2014-07-27 23:38:49      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:android   sqlite   

前言:因为找了N多网上的资源都没有好的解决方案,别人都是只给思路没给具体源码,真TMD纠结,干嘛求别人,自己动手才是真,最痛恨那些所谓大牛的作风,给了点点代码就让别人去想,你让我们这种小白情何于堪!!!!!!此例是基于listview来实现本地sqlite实现的!

二话不说,程序猿求的是有图有真相有源码!大家下载后有什么问题可以找到本人;QQ508181017

bubuko.com,布布扣

核心代码如下

1、数据库操作类

package com.icq.demo.db;

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

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.icq.demo.R;
import com.icq.demo.beans.Item;

public class BaseDBDao {
	private static String dbname="icq_db";
	private static GYMDatabase mdb;
	private static SQLiteDatabase db;
	private static String apppackage="com.icq.demo";
	private static int dbRec=R.raw.icq_db;

    /**
     * 获取省
     * @param context
     * @return
     */
	public static List<Item> getProvinces(Context context){
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List<Item> list = new ArrayList<Item>();
		try {
			String sql = "select * from province";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			for (int i = 0; i < cursor.getCount(); i++) {
				String code = cursor.getString(cursor
						.getColumnIndex("ProvinceID"));
				String name = cursor.getString(cursor
						.getColumnIndex("ProvinceName"));
				Item myListItem = new Item();
				myListItem.setName(name);
				myListItem.setId(code);
				list.add(myListItem);
				cursor.moveToNext();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		mdb.closeDatabase();
		db.close();
		
		return list;
	}
	

	/**
	 * 根据省级id获取城市列表
	 * @param pid
	 * @param context
	 * @return
	 */
	public static List<Item> getCitys(String pid,Context context) {
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List<Item> list = new ArrayList<Item>();

		try {
			String sql = "select * from city where ProvinceID='" + pid
					+ "'";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			for (int i = 0; i < cursor.getCount(); i++) {
				String code = cursor.getString(cursor.getColumnIndex("CityID"));
				String name = cursor.getString(cursor.getColumnIndex("CityName"));
				Item myListItem = new Item();
				myListItem.setName(name);
				myListItem.setId(code);
				list.add(myListItem);
				cursor.moveToNext();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		mdb.closeDatabase();
		db.close();

		return list;
	}
	/**
	 * 根据城市id获取城镇
	 * @param cid
	 * @param context
	 * @return
	 */
	public static List<Item> getCountrys(String cid,Context context) {
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List<Item> list = new ArrayList<Item>();

		try {
			String sql = "select * from district where CityID='" + cid
					+ "'";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			for (int i = 0; i < cursor.getCount(); i++) {
				String code = cursor.getString(cursor.getColumnIndex("DistrictID"));
				String name = cursor.getString(cursor.getColumnIndex("DistrictName"));
				Item myListItem = new Item();
				myListItem.setName(name);
				myListItem.setId(code);
				list.add(myListItem);
				cursor.moveToNext();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		mdb.closeDatabase();
		db.close();

		return list;
	}
	

}

2、适配器

public class ListAdapter extends BaseAdapter {
	private Context context;
	private List<Item> myList;
	private LayoutInflater mInflater;
	private int selectedPosition = -1;// 选中的位置
	public ListAdapter(Context context, List<Item> myList,int selected) {
		this.context = context;
		this.myList = myList;
		this.mInflater = LayoutInflater.from(this.context);
		selectedPosition=selected;
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	public void setSelectedPosition(int position) {
		selectedPosition = position;
	}

	public View getView(final int position,  View convertView, final ViewGroup parent) {
		
		 ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.block_list_item, null);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final View cView=convertView;
		final ViewHolder cHolder=holder;
		holder.iLayout=(LinearLayout) convertView.findViewById(R.id.ll_root);
		holder.name = (TextView) convertView.findViewById(R.id.tv_name);
		holder.img = (ImageView) convertView.findViewById(R.id.iv_right);
		holder.name.setText(myList.get(position).getName());
		System.out.println("Item值为:"+selectedPosition);
		System.out.println("自带的为:"+position);
		if (selectedPosition == position) {
			holder.iLayout.setSelected(true);
			holder.iLayout.setPressed(true);
			holder.iLayout.setBackgroundColor(Color.RED);
		} else {
			holder.iLayout.setSelected(false);
			holder.iLayout.setPressed(false);
			holder.iLayout.setBackgroundColor(Color.TRANSPARENT);   

		}

		return convertView;
	}
	
	class ButtonView {
		int layoutViewId;

		ButtonView(int tId) {
			layoutViewId = tId;
		}
	}
     

	public final class ViewHolder {
		public TextView name;
		public ImageView img;
		public LinearLayout iLayout;
	}

}

3、xml视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="#1E90FF" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:text="多级菜单"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="400dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list_province"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="400dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list_city"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="400dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list_district"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <LinearLayout
        android:id="@+id/ll_root"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxEms="18"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="10dp"
            android:singleLine="true"
            android:text="list方式展示"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/submenu_right_default" />
    </LinearLayout>

</LinearLayout>

4、操作界面

public class ListViewShowActivity extends Activity{
	private Context mContext;
	
	private ListView mLv_province,mLv_city,mLv_district;
	private ListAdapter mProvinceAdapter,mCityAdapter,mDistrictAdapter;
	private List<Item> provinces,cities,districts;
	
	private String pId,cId,dId;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview);
		mContext=this;
		initView();
		initData();
		initOnItemClick();
		
	}



	private void initOnItemClick() {
		mLv_province.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				mProvinceAdapter.setSelectedPosition(position);
				mProvinceAdapter.notifyDataSetInvalidated(); 
				
				
				Item proItem=(Item) mProvinceAdapter.getItem(position);
				pId=proItem.getId();
				//二级动
				cities=BaseDBDao.getCitys(pId, mContext);
				mCityAdapter=new ListAdapter(mContext, cities,0);
				mCityAdapter.notifyDataSetChanged();
				mLv_city.setAdapter(mCityAdapter);
				
				//三级动
				districts=BaseDBDao.getCountrys(cities.get(0).getId(), mContext);
				mDistrictAdapter=new ListAdapter(mContext, districts,0);
				mDistrictAdapter.notifyDataSetChanged();
				mLv_district.setAdapter(mDistrictAdapter);
				
			}
		});
		mLv_city.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				
				mCityAdapter.setSelectedPosition(position);
				mCityAdapter.notifyDataSetInvalidated(); 
				
				Item proItem=(Item) mCityAdapter.getItem(position);
				cId=proItem.getId();
				
				districts=BaseDBDao.getCountrys(cId, mContext);
				mDistrictAdapter=new ListAdapter(mContext, districts,0);
				mDistrictAdapter.notifyDataSetChanged();
				mLv_district.setAdapter(mDistrictAdapter);
				
				
			}
		});
		
		
		mLv_district.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				mDistrictAdapter.setSelectedPosition(position);
				mDistrictAdapter.notifyDataSetInvalidated(); 
				
				Item proItem=(Item) mDistrictAdapter.getItem(position);
				dId=proItem.getId();
				
			}
		});
		
		
		
	}



	private void initData() {
		provinces=BaseDBDao.getProvinces(mContext);
		mProvinceAdapter=new ListAdapter(mContext, provinces,0);
		mLv_province.setAdapter(mProvinceAdapter);
		
		cities=BaseDBDao.getCitys(provinces.get(0).getId(), mContext);
		mCityAdapter=new ListAdapter(mContext, cities,0);
		mLv_city.setAdapter(mCityAdapter);
		
		districts=BaseDBDao.getCitys(cities.get(0).getId(), mContext);
		mDistrictAdapter=new ListAdapter(mContext, districts,0);
		mLv_district.setAdapter(mDistrictAdapter);
		
	}



	private void initView() {
		mLv_province=(ListView) findViewById(R.id.list_province);
		mLv_city=(ListView) findViewById(R.id.list_city);
		mLv_district=(ListView) findViewById(R.id.list_district);
	}


	


}

因为资源上传太久,估计是留了我个人的联系信息管理员封了,QQ联系我吧,发资源给大家,或者资源审核通过了再上下载资源地址;QQ508181017

已经审核通过,确实是包含联系方式问题才不能上传,后面修改了:http://download.csdn.net/detail/shi508181017/7684909

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色,布布扣,bubuko.com

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色

标签:android   sqlite   

原文地址:http://blog.csdn.net/icqapp/article/details/38187151

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!