Android Studio 繫結下拉框資料(網路JSON資料)
阿新 • • 發佈:2019-01-28
效果顯示:
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);
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) {
}
});