Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android listview級聯三菜單選擇地區,本地數據庫sqlite級聯地區,item選中不變色

android listview級聯三菜單選擇地區,本地數據庫sqlite級聯地區,item選中不變色

編輯:關於Android編程

前言:因為找了N多網上的資源都沒有好的解決方案,別人都是只給思路沒給具體源碼,真TMD糾結,干嘛求別人,自己動手才是真,最痛恨那些所謂大牛的作風,給了點點代碼就讓別人去想,你讓我們這種小白情何於堪!!!!!!此例是基於listview來實現本地sqlite實現的!

二話不說,程序猿求的是有圖有真相有源碼!大家下載後有什麼問題可以找到本人;QQ508181017

\

核心代碼如下

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 getProvinces(Context context){
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List list = new ArrayList();
		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 getCitys(String pid,Context context) {
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List list = new ArrayList();

		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 getCountrys(String cid,Context context) {
		mdb = new GYMDatabase(context, dbname, apppackage, dbRec);
		mdb.openDatabase();
		db = mdb.getDatabase();
		List list = new ArrayList();

		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 myList;
	private LayoutInflater mInflater;
	private int selectedPosition = -1;// 選中的位置
	public ListAdapter(Context context, List 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視圖

 

 



    

        
    

    

        

            
            
        

        

            
            
        

        

            
            
        
    




    

        

        
    


4、操作界面

 

 

public class ListViewShowActivity extends Activity{
	private Context mContext;
	
	private ListView mLv_province,mLv_city,mLv_district;
	private ListAdapter mProvinceAdapter,mCityAdapter,mDistrictAdapter;
	private List 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);
	}


	


}

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved