亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android Studio綁定下拉框數(shù)據(jù)詳解

 更新時間:2017年10月13日 09:25:18   作者:龍魂學者  
這篇文章主要為大家詳細介紹了Android Studio綁定下拉框數(shù)據(jù),Android Studio綁定網(wǎng)絡JSON數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下

效果顯示:

這里寫圖片描述
這里寫圖片描述

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創(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;//獲取第一個醫(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);
     //設置樣式
     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)絡,稍后重試", Toast.LENGTH_SHORT).show();
  }
 });
}


5、創(chuàng)建點擊事件

//醫(yī)生選擇點擊事件
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論