Android Studio綁定下拉框數(shù)據(jù)詳解
效果顯示:
1、頁面xml代碼(項(xiàng)目的代碼,直接復(fù)制會(huì)有錯(cuò),自己修改一下就好)
<TextView android:id="@+id/consultation_tv_section" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/consultation_et_doctor" android:layout_alignLeft="@+id/consultation_tv_phone" android:layout_alignStart="@+id/consultation_tv_phone" android:layout_marginBottom="8dp" android:text="掛號科室:" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <Spinner android:id="@+id/consultation_et_section" android:layout_width="260dp" android:layout_height="40dp" android:layout_alignLeft="@+id/consultation_et_phone" android:layout_alignStart="@+id/consultation_et_phone" android:layout_below="@+id/consultation_et_phone" android:layout_marginTop="22dp" android:background="@drawable/input_bg" android:ems="10" android:inputType="textPersonName" android:paddingLeft="15dp" android:paddingRight="15dp" android:spinnerMode="dialog" />
2、java創(chuàng)建自定義參數(shù)
Spinner consultation_et_section;//掛號科室:
3、獲取選擇控件(Spinner)
consultation_et_section = (Spinner) findViewById(R.id.consultation_et_section);
4、獲取JSON數(shù)據(jù)和綁定數(shù)據(jù),可以參考前一篇文章
(1)、創(chuàng)建自定義參數(shù)
List<String> listMemDoctorData = 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ù)源 ArrayAdapter<String> arrayAdapter = 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ù)請求失敗,請稍后重試", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { //請求失敗的回調(diào)處理 Toast.makeText(GV_Consultation.this, "請鏈接網(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android DataBinding單向數(shù)據(jù)綁定深入探究
- 淺析Android企業(yè)級開發(fā)數(shù)據(jù)綁定技術(shù)
- 詳解Android的MVVM框架 - 數(shù)據(jù)綁定
- Android Data Binding數(shù)據(jù)綁定詳解
- Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
- Android ListView數(shù)據(jù)綁定顯示的三種解決方法
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場景
相關(guān)文章
Android獲取assets文件夾中的數(shù)據(jù)并寫入SD卡示例
這篇文章主要介紹了Android獲取assets文件夾中的數(shù)據(jù)并寫入SD卡示例,對初學(xué)Android開發(fā)的朋友來說是一個(gè)很實(shí)用的功能,需要的朋友可以參考下2014-07-07Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03Android開發(fā)服務(wù)Service全面講解
Android的服務(wù)是開發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒有接口,生命周期也與活動(dòng)Activity非常不同。通過使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁等,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考下2023-02-02Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05以一個(gè)著色游戲展開講解Android中區(qū)域圖像填色的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)區(qū)域圖像顏色填充的方法,文中以一個(gè)著色游戲?yàn)槔v解了邊界的填充等各種填色操作,需要的朋友可以參考下2016-02-02Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案
隨著Android手機(jī)的越來越先進(jìn),給我們開發(fā)者而言傳遞的圖片也是越來越大,這個(gè)時(shí)候我們可以對一些沒有必要原圖展示的圖片進(jìn)行壓縮,這篇文章主要給大家介紹了關(guān)于Android性能優(yōu)化之圖片大小,尺寸壓縮的綜合解決方案,需要的朋友可以參考下2022-04-04Android中ExpandableListView使用示例詳解
這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10