本篇文章為大家展示了Android Studio中怎么綁定下拉框數(shù)據(jù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)專注服務(wù)器托管服務(wù)十載,提供服務(wù)器租用、虛擬空間、申請(qǐng)域名、云服務(wù)器、云主機(jī)租用、成都IDC機(jī)房托管、建站等服務(wù)
1、頁面xml代碼
2、java創(chuàng)建自定義參數(shù)
Spinner consultation_et_section;//掛號(hào)科室:
3、獲取選擇控件(Spinner)
consultation_et_section = (Spinner) findViewById(R.id.consultation_et_section);
4、獲取JSON數(shù)據(jù)和綁定數(shù)據(jù),可以參考前一篇文章
(1)、創(chuàng)建自定義參數(shù)
ListlistMemDoctorData = null;
(2)、獲取數(shù)據(jù)和綁定數(shù)據(jù)
/** * 獲取JSON醫(yī)生信息數(shù)據(jù) */ public void findDoctorData(int sectionId){ AsyncHttpClient client = new AsyncHttpClient(); client.get(AbAppConfig.DATA_URL + "appGVConsultation/findDoctorData?sectionId="+sectionId, null, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { try { JSONObject object = new JSONObject(new String(responseBody));//獲取json數(shù)據(jù) JSONArray jsonArray = object.getJSONArray("obj");//獲取數(shù)據(jù)集名稱為obj的數(shù)據(jù) Log.d("jsonArray數(shù)據(jù)輸出:", String.valueOf(jsonArray)); listMemDoctor = new ArrayList<>(); for (int i = 0; i < jsonArray.length();i++) { MemDoctor doctor = MemDoctor.doctorData(jsonArray.getJSONObject(i));//把數(shù)據(jù)存在novels集合中 if (doctor != null){ listMemDoctor.add(doctor); } } if (jsonArray.length() > 0){ listMemDoctorData = new ArrayList<>(); doctor_id = listMemDoctor.get(0).id;//獲取第一個(gè)醫(yī)生的ID for (int i = 0; i < listMemDoctor.size(); i++){ MemDoctor section = listMemDoctor.get(i); listMemDoctorData.add(section.doctorName); } //建立 Adapter并且綁定數(shù)據(jù)源 ArrayAdapterarrayAdapter = new ArrayAdapter<>(GV_Consultation.this, android.R.layout.simple_spinner_item, listMemDoctorData); //設(shè)置樣式 arrayAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice); //綁定 Adapter到控件 consultation_et_doctor.setAdapter(arrayAdapter); }else { consultation_et_doctor.setAdapter(null); doctor_id = 0; Toast.makeText(GV_Consultation.this, "該科室沒有醫(yī)生信息數(shù)據(jù)", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { Toast.makeText(GV_Consultation.this, "數(shù)據(jù)請(qǐng)求失敗,請(qǐng)稍后重試", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { //請(qǐng)求失敗的回調(diào)處理 Toast.makeText(GV_Consultation.this, "請(qǐng)鏈接網(wǎng)絡(luò),稍后重試", Toast.LENGTH_SHORT).show(); } }); }
5、創(chuàng)建點(diǎn)擊事件
//醫(yī)生選擇點(diǎn)擊事件 consultation_et_doctor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> parent, View view, int pos, long id) { doctor_id = listMemDoctor.get((int) id).id;//獲取選擇醫(yī)生的ID } @Override public void onNothingSelected(AdapterView> parent) { } });
上述內(nèi)容就是Android Studio中怎么綁定下拉框數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。