Android Studio绑定下拉框数据详解
程序员文章站
2023-12-10 13:52:10
效果显示:
1、页面xml代码(项目的代码,直接复制会有错,自己修改一下就好)
效果显示:
1、页面xml代码(项目的代码,直接复制会有错,自己修改一下就好)
<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创建自定义参数
spinner consultation_et_section;//挂号科室:
3、获取选择控件(spinner)
consultation_et_section = (spinner) findviewbyid(r.id.consultation_et_section);
4、获取json数据和绑定数据,可以参考
(1)、创建自定义参数
list<string> listmemdoctordata = null;
(2)、获取数据和绑定数据
/** * 获取json医生信息数据 */ 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数据 jsonarray jsonarray = object.getjsonarray("obj");//获取数据集名称为obj的数据 log.d("jsonarray数据输出:", string.valueof(jsonarray)); listmemdoctor = new arraylist<>(); for (int i = 0; i < jsonarray.length();i++) { memdoctor doctor = memdoctor.doctordata(jsonarray.getjsonobject(i));//把数据存在novels集合中 if (doctor != null){ listmemdoctor.add(doctor); } } if (jsonarray.length() > 0){ listmemdoctordata = new arraylist<>(); doctor_id = listmemdoctor.get(0).id;//获取第一个医生的id for (int i = 0; i < listmemdoctor.size(); i++){ memdoctor section = listmemdoctor.get(i); listmemdoctordata.add(section.doctorname); } //建立 adapter并且绑定数据源 arrayadapter<string> arrayadapter = new arrayadapter<>(gv_consultation.this, android.r.layout.simple_spinner_item, listmemdoctordata); //设置样式 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, "该科室没有医生信息数据", toast.length_short).show(); } } catch (jsonexception e) { toast.maketext(gv_consultation.this, "数据请求失败,请稍后重试", toast.length_short).show(); } } @override public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) { //请求失败的回调处理 toast.maketext(gv_consultation.this, "请链接网络,稍后重试", toast.length_short).show(); } }); }
5、创建点击事件
//医生选择点击事件 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;//获取选择医生的id } @override public void onnothingselected(adapterview<?> parent) { } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。