Spinner+sqlite 三级联动

前端之家收集整理的这篇文章主要介绍了Spinner+sqlite 三级联动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
品牌表: CREATE TABLE [pinpai] ( [ID] INT(4),[NAME] nvarchar,[ZM] nchar,[NamePY] BOOLEAN(1)); insert into [pinpai] values(1,'奥迪','A',0); insert into [pinpai] values(2,'宝马','B',0); insert into [pinpai] values(3,'北京',0); insert into [pinpai] values(4,'奔驰',0); 。。。。。 车系表: CREATE TABLE [chexi] ( [id] INT(4),[ppid] INT(4),[cxname] nvarchar(50),[ParentID] INT(4),[px] INT(4)); insert into [chexi] values(1,1,'==奥迪==',1); insert into [chexi] values(2,'奥迪A6L',2); insert into [chexi] values(3,'奥迪A4L',3); insert into [chexi] values(4,'奥迪Q5',4); insert into [chexi] values(5,2,'==华晨宝马==',5); insert into [chexi] values(6,'宝马3系',6); insert into [chexi] values(7,'宝马5系',7); insert into [chexi] values(8,3,'==北汽制造==',8); insert into [chexi] values(9,'角斗士',9); insert into [chexi] values(10,'京城海狮',10); insert into [chexi] values(11,'路霸',11); insert into [chexi] values(12,'骑士',12); insert into [chexi] values(13,'勇士',13); insert into [chexi] values(14,'战旗',14); 。。。。。 车型表: CREATE TABLE [chexing] ( [ID] INT(4),[chexiid] INT(4),[year] nchar,[jg] money); insert into [chexing] values(1,'新A6L 2.0 TFSI AT','2009',39.8); insert into [chexing] values(2,'新A6L 2.0 TFSI MT',34.82); insert into [chexing] values(3,'新A6L 2.4 AT/MT',42.45); insert into [chexing] values(4,'新A6L 2.8 FSI AT/MT',61.82); insert into [chexing] values(5,'新A6L 3.0 TFSI AT/MT',69.6); insert into [chexing] values(6,'2010',39.99); insert into [chexing] values(7,35.5); insert into [chexing] values(8,43.15); insert into [chexing] values(9,'新A6L 2.7 TDI AT/MT',48.88); insert into [chexing] values(10,53.77); insert into [chexing] values(11,'新A6L 3.0 TFSI quattro AT/MT',69.6); insert into [chexing] values(12,'A4L 2.0 TFSI',38.86); insert into [chexing] values(13,'A4L 3.2 FSI quattro',53.88); insert into [chexing] values(14,'A4L 1.8 TFSI',29.1); insert into [chexing] values(15,32.99); insert into [chexing] values(16,57.81); insert into [chexing] values(17,4,'Q5 2.0T FSI',0); insert into [chexing] values(18,'Q5 3.2 FSI',0); insert into [chexing] values(19,6,'318i 2.0 AT/MT',30.6); insert into [chexing] values(20,'320i 2.0 AT/MT',37.5); insert into [chexing] values(21,'325i 2.5 AT/MT',44.2); insert into [chexing] values(22,30.6); insert into [chexing] values(23,37.5); insert into [chexing] values(24,44.2); insert into [chexing] values(25,7,'520Li',42.36); insert into [chexing] values(26,'523Li',43.36); insert into [chexing] values(27,'530Li',68.86); 。。。。 定义查询语句 public static final String PINPAIsql="select id as _id,zm||'-'|| NAME as ppnamefrom pinpai where namepy=? order by zm"; public static final String CHEXIsql="select id as _id,cxnamefrom chexi where ppid=?order by px"; public static final String CHEXINGsql="select id as _id,cxname||'['||[year]||']'||'$'||jg as mingcheng from chexing where chexiid=?order by [year],cxname"; 点击按钮 scarButton=(Button)findViewById(R.id.widget622); scarButton.setOnClickListener(new sCarButtonListener()); 单击事件 private class sCarButtonListener implements android.view.View.OnClickListener { public void onClick(View v) { showDialog_SCar(); } } 弹出对话框 private void showDialog_SCar() { LayoutInflater inflater = LayoutInflater.from(this); final View textEntryView = inflater.inflate( R.layout.scar,null); pinpaiSpinner=(Spinner)textEntryView.findViewById(R.id.widget36); chexiSpinner=(Spinner)textEntryView.findViewById(R.id.widget39); chexingSpinner=(Spinner)textEntryView.findViewById(R.id.widget42); pinpaiSpinner.setAdapter(Comm.GetDataBysql(this,Comm.PINPAIsql,new String[] {"0"},new String[] {"ppname"})); pinpaiSpinner.setPrompt("请选择"); //品牌选择事件 pinpaiSpinner.setOnItemSelectedListener(newSpinner.OnItemSelectedListener(){ public voidonItemSelected(AdapterView<?> arg0,View arg1,intarg2,longarg3) { //根据品牌id--arg3 绑定车系 CheXiAdapter=Comm.GetDataBysql(ActDingSun.this,Comm.CHEXIsql,new String[] {arg3+""},new String[] {"cxname"}); chexiSpinner.setAdapter(CheXiAdapter); chexiSpinner.setPrompt("请选择"); /* 将mySpinner 显示*/ arg0.setVisibility(View.VISIBLE); } public voidonNothingSelected(AdapterView<?> arg0) { } }); //选择车系事件 chexiSpinner.setOnItemSelectedListener(newSpinner.OnItemSelectedListener(){ public voidonItemSelected(AdapterView<?> arg0,longarg3) { //根据车系id 绑定车型 CheXingAdapter=Comm.GetDataBysql(ActDingSun.this,Comm.CHEXINGsql,new String[] {"mingcheng"}); chexingSpinner.setAdapter(CheXingAdapter); chexingSpinner.setPrompt("请选择"); //chexiSpinner.setSelection(0,true); /* 将mySpinner 显示*/ arg0.setVisibility(View.VISIBLE); } public voidonNothingSelected(AdapterView<?> arg0) { } }); final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(false); builder.setIcon(R.drawable.icon); builder.setTitle("选择车型"); builder.setView(textEntryView); builder.setPositiveButton("确认",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { } }); builder.setNegativeButton("取消",int whichButton) { } }); builder.show(); } //返回SimpleCursorAdapter 填充spinner public static SimpleCursorAdapter GetDataBysql(Context context,String sqlString,String [] args,String [] argsc){ SimpleCursorAdapter scaCategories=null; DatabaseHelper dHelper=new DatabaseHelper(context); sqliteDatabase db= dHelper.getReadableDatabase(); Cursor msCur= db.rawQuery(sqlString,args); if (msCur.getCount() != 0) { scaCategories = new SimpleCursorAdapter(context,android.R.layout.simple_spinner_item,msCur,argsc,new int[]{android.R.id.text1}); scaCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); } return scaCategories; } 原文链接:https://www.f2er.com/sqlite/202326.html

猜你在找的Sqlite相关文章