1. 程式人生 > >側滑選單+三級聯動搜尋下拉框

側滑選單+三級聯動搜尋下拉框

//側滑選單 mDrawerLayout = (DrawerLayout) findViewById(R.id.professional_layout); mDrawerLayout.openDrawer(Gravity.RIGHT);//開啟側滑選單 spinnerProfessional = (Spinner)findViewById(R.id.spinner);//下拉框1 spinnerSecondchoice = (Spinner)findViewById(R.id.spinner2);//下拉框2 spinnerSmallestchoice = (Spinner)findViewById(R.id.spinner3
);//下拉框3 Button button_query=(Button)findViewById(R.id.button_query);//確定按鈕 Button button_cancel=(Button)findViewById(R.id.button_cancel);//取消按鈕 primaryScreening=(EditText)findViewById(R.id.primaryScreening);//搜尋框 arrayList_data = new ArrayList<String>();//搜尋資訊比對的資料 professional_data = new ArrayList<String>();//
搜尋顯示的資料 professionalAdapter = new ArrayAdapter<String>(EShopTYGR_EPAYActivity.this, android.R.layout.simple_spinner_item, Util.province); //設定下拉列表的風格 professionalAdapter.setDropDownViewResource(android.R.layout.select_dialog_item); spinnerProfessional.setAdapter(professionalAdapter
); spinnerProfessional.setSelection(professionalPosition,true); //預設選中第professionalPositionsecondchoiceAdapter = new ArrayAdapter<String>(EShopTYGR_EPAYActivity.this, android.R.layout.simple_spinner_item, Util.city[professionalPosition]); //設定下拉列表的風格 secondchoiceAdapter.setDropDownViewResource(android.R.layout.select_dialog_item); spinnerSecondchoice.setAdapter(secondchoiceAdapter); spinnerSecondchoice.setSelection(secondchoicePosition,true); //預設選中第0smallestchoiceAdapter = new ArrayAdapter<String>(EShopTYGR_EPAYActivity.this, android.R.layout.simple_spinner_item, Util.county[professionalPosition][secondchoicePosition]); //設定下拉列表的風格 smallestchoiceAdapter.setDropDownViewResource(android.R.layout.select_dialog_item); spinnerSmallestchoice.setAdapter(smallestchoiceAdapter); spinnerSmallestchoice.setSelection(smallestchoicePosition,true); //預設選中第0spinnerProfessional.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { // 表示選項被改變的時候觸發此方法,主要實現辦法:動態改變二級職業介面卡的繫結值 @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { //position為當前一級職業選中的值的序號 //將二級職業介面卡的值改變為city[position]中的值 secondchoiceAdapter = new ArrayAdapter<String>( EShopTYGR_EPAYActivity.this, android.R.layout.simple_spinner_item, Util.city[position]); secondchoiceAdapter.setDropDownViewResource(android.R.layout.select_dialog_item); // 設定二級下拉列表的選項內容介面卡 spinnerSecondchoice.setAdapter(secondchoiceAdapter); if(isSearch){//是搜尋顯示搜尋中選擇的值 spinnerSecondchoice.setSelection(index_j,true); //預設選中第index_j}else{//不是搜尋顯示secondchoicePosition的值 spinnerSecondchoice.setSelection(secondchoicePosition,true); } professionalPosition = position; //記錄當前序號,留給下面修改三級職業介面卡時用 } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); //二級職業下拉監聽 spinnerSecondchoice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { smallestchoiceAdapter = new ArrayAdapter<String>(EShopTYGR_EPAYActivity.this, android.R.layout.simple_spinner_item, Util.county[professionalPosition][position]); smallestchoiceAdapter.setDropDownViewResource(android.R.layout.select_dialog_item); spinnerSmallestchoice.setAdapter(smallestchoiceAdapter); if(isSearch){ spinnerSmallestchoice.setSelection(index_k,true); isSearch=false; }else{ spinnerSmallestchoice.setSelection(smallestchoicePosition,true); } secondchoicePosition= position; //清除搜尋資料 index_i=0; index_j=0; index_k=0; } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); spinnerSmallestchoice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { smallestchoicePosition= position; } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); //確定按鈕 button_query.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String strProfessional=spinnerProfessional.getSelectedItem().toString();//獲取當前下拉框1的資訊 String strSecondchoice=spinnerSecondchoice.getSelectedItem().toString(); String strSmallestchoice=spinnerSmallestchoice.getSelectedItem().toString(); Util.findAllHolderSetText(mainHolder, "insd_isdProfessional_1",strProfessional); Util.findAllHolderSetText(mainHolder, "insd_isdSecondchoice_1",strSecondchoice); Util.findAllHolderSetText(mainHolder, "insd_isdSmallestchoice_1",strSmallestchoice); String str=Util.getProfessional(strProfessional,strSecondchoice,professionalPosition, secondchoicePosition,smallestchoicePosition);//獲取職業相關職業程式碼 Util.findAllHolderSetText(mainHolder, "Professional_1",str); primaryScreening.setText("");//情空搜尋框資料 Toast.makeText(EShopTYGR_EPAYActivity.this,str,Toast.LENGTH_LONG).show(); mDrawerLayout.closeDrawers();//沒有引數,關閉所有的出現的抽屜 } }); button_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { primaryScreening.setText(""); mDrawerLayout.closeDrawers();//沒有引數,關閉所有的出現的抽屜 } }); // 搜尋框的鍵盤搜尋鍵點選回撥 primaryScreening.setOnKeyListener(new View.OnKeyListener() {// 輸入完後按鍵盤上的搜尋鍵 public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 修改回車鍵功能 // 先隱藏鍵盤 ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow( getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); // 匹配輸入的職業 String strSearch=primaryScreening.getText().toString().trim(); if(strSearch.equals("") || strSearch.length()==0){//判斷搜尋框是否為空 return false; } int index=0; for(int i=0;i<Util.province.length;i++){ for (int j=0;j<Util.city[i].length;j++){ for (int k=0;k<Util.county[i][j].length;k++){//三級職業 if(Util.county[i][j][k].contains(strSearch)){//搜尋內容在三級職業查詢 positionSearch_1.add(index,i); positionSearch_2.add(index,j); positionSearch_3.add(index,k); index++; }else if(Util.city[i][j].contains(strSearch)){//搜尋內容在二級職業查詢 positionSearch_1.add(index,i); positionSearch_2.add(index,j); positionSearch_3.add(index,k); index++; } } } } if(index==0){ Toast.makeText(EShopTYGR_EPAYActivity.this,"沒有查詢到該職業!請重新輸入。",Toast.LENGTH_LONG).show(); return false; } //顯示篩選資料 AlertDialog.Builder builder = new AlertDialog.Builder(EShopTYGR_EPAYActivity.this); final View viewTile = LayoutInflater.from(EShopTYGR_EPAYActivity.this).inflate(R.layout.custom_title, null); TextView titleName = (TextView) viewTile.findViewById(R.id.tvname); titleName.setText("選擇職業"); builder.setCustomTitle(viewTile);//設定標題佈局 final View province_list = LayoutInflater.from(EShopTYGR_EPAYActivity.this).inflate(R.layout.province_list, null); ListView listView=(ListView)province_list.findViewById(R.id.list_item); listView.setAdapter(new ArrayAdapter<String>(EShopTYGR_EPAYActivity.this, android.R.layout.simple_expandable_list_item_1,getData(index))); builder.setView(province_list);//設定dialog佈局 province_dialog =builder.show();//獲取dialog準備dismiss listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //判斷搜尋內容 if(arrayList_data.get(position).equals(Util.county[positionSearch_1.get(position)][positionSearch_2.get(position)][positionSearch_3.get(position)])){ index_i=positionSearch_1.get(position);//獲取當前下拉框1選擇值的序號 index_j=positionSearch_2.get(position); index_k=positionSearch_3.get(position); } //改變預設值觸發下拉框的onItemSelected if(index_i==0){ spinnerProfessional.setSelection(1,true); }else{ spinnerProfessional.setSelection(0,true); } spinnerProfessional.setSelection(index_i,true); //預設選中第0//清除資料關閉視窗 province_dialog.dismiss(); positionSearch_1.clear(); positionSearch_2.clear(); positionSearch_3.clear(); arrayList_data.clear(); professional_data.clear(); //判斷是否使用搜索功能 isSearch=true; } }); } return false; } }); } //搜尋資料 private ArrayList<String> getData(int index){ for (int i=0;i<index;i++){ String strProfessional=Util.province[positionSearch_1.get(i)]+"-" +Util.city[positionSearch_1.get(i)][positionSearch_2.get(i)]+"-" +Util.county[positionSearch_1.get(i)][positionSearch_2.get(i)][positionSearch_3.get(i)]; arrayList_data.add(Util.county[positionSearch_1.get(i)][positionSearch_2.get(i)][positionSearch_3.get(i)]); professional_data.add(strProfessional); } return professional_data; }