效果显示:
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) { } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。