Android ListView Cascade Three menu Select region, Local database sqlite cascading region, item selected does not change color

Source: Internet
Author: User

Foreword: Because found n many online resources there is no good solution, others are only to the idea did not give specific source code, really TMD tangled, why beg others, their hands is true, the most hated the so-called Daniel Style, give a little bit of code let others think, you let us this small white love what!!!!!! This example is based on the ListView to implement local SQLite!

Apart, the procedure ape asks is has the figure has the truth to have the source code! Everyone download after the problem can find himself; QQ508181017


The core code is as follows

1. Database Operation class

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";p rivate static Gymdatabase mdb;    private static sqlitedatabase db;private static String apppackage= "Com.icq.demo";p rivate static int dbrec=r.raw.icq_db; /** * Get Province * @param context * @return */public static list<item> getprovinces (context context) {MDB = new Gymdatabase (context, dbname, Apppackage, Dbrec), mdb.opendatabase ();d B = 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 ();d b.close (); return list;} /** * Get city list according to provincial ID * @param PID * @param context * @return */public static list<item> Getcitys (String Pid,context Co ntext) {mdb = new Gymdatabase (context, dbname, Apppackage, Dbrec); Mdb.opendatabase ();d B = mdb.getdatabase (); list<item> list = new arraylist<item> (); try {String sql = "SELECT * from city where provinceid= '" + pid+ "'"; C Ursor cursor = db.rawquery (sql, NULL); Cursor.movetofirst (); for (int i = 0; i < Cursor.getcount (); i++) {String code = C Ursor.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 ();d b. Close (); return list;} /** * Get Town * @param CID * @param context * @return */public static list<item> Getcountrys (String cid,context c) according to City ID Ontext) {mdb = new Gymdatabase (context, dbname, Apppackage, Dbrec); Mdb.opendatabase ();d B = mdb.getdatabase (); list<item> list = new arraylist<item> (); try {String sql = "Select * from district where cityid= '" + cid+ "'"; C Ursor cursor = db.rawquery (sql, NULL); Cursor.movetofirst (); for (int i = 0; i < Cursor.getcount (); i++) {String code = C Ursor.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 ();d b.close (); return list;}}

2. Adapter

public class ListAdapter extends Baseadapter {private Context context;private list<item> mylist;private Layoutinflater minflater;private int selectedposition = -1;//selected Location 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 (CONVERTV Iew = = 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 Value:" +selectedposition); System.out.println ("comes with:" +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 view

<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:orienta        tion= "vertical" > <relativelayout android:layout_width= "fill_parent" android:layout_height= "45DP" android:background= "#1E90FF" > <textview android:layout_width= "wrap_content" Android oid:layout_height= "Wrap_content" android:layout_centervertical= "true" android:layout_marginleft= "20d P "android:text=" multilevel Menu "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" an Droid:layout_height= "400DP" Android:layout_weight= "1" android:orientation= "vertical" > <listview android:id= "@+id/l Ist_province "android:scrollbars=" None "Android:layout_width=" Match_parent "a             ndroid: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        Oid: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" Andro id:layout_height= "fill_parent" android:gravity= "center" > <textview android:id= "@+id/tv_nam E "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight= "1" android:ellipsize= "End" android:maxems= "android:paddingbottom=" 10DP "a ndroid:paddingleft= "5DP" android:paddingtop= "10DP" android:singleline= "true" Android:text            = "List mode display" 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& Gt;</linearlayout>

4. Operation interface

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; @Overrideprotected 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 () {@Overridepublic void Onitemclick (adapterview<?> adapter, view view, int Position,long id) {mprovinceadapter.setselectedposition ( position); mprovinceadapter.notifydatasetinvalidated (); Item proitem= (item) mprovinceadapter.getitem (position);p Id=proitem.getid ();//Two-stage dynamic Cities=basedbdao.getcitys (PId, Mcontext); mcityadapter=new listadapter (Mcontext, cities,0); mcityadapter.notifydatasetchanged (); mLv_ City.setadapter (mcityadapter);//three-stage dynamic 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 () {@Overridepublic void Onitemclick (adapterview<?> Adapter, view view, int Position,long id) {mcityadapter.setselectedposition (position); Mcityadapter.notifydatasetinvalidated (); Item proitem= (item) mcityadapter.getitem (position); Cid=proitem.getid ();d Istricts=basedbdao.getcountrys (CId, Mcontext); mdistrictadapter=new listadapter (Mcontext, districts,0); mdistrictadapter.notifydatasetchanged (); mLv_ District.setadapter (Mdistrictadapter);}}); Mlv_district.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<? > Adapter, view view, int Position,long id) {mdistrictadapter.setselectedposition (position); Mdistrictadapter.notifydatasetinvalidated (); Item proitem= (item) Mdistrictadapter.getitem(position);d Id=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);d istricts= 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);}}

because the resources uploaded too long, it is estimated to have left my personal contact information manager closed, QQ contact me, send resources to everyone, or resource audit through the download resource address; QQ508181017 has been approved by, it is not included in the contact method issue can not be uploaded, later modified: http://download.csdn.net/detail/shi508181017/7684909

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.