AndroidStudio中怎么綁定下拉框數(shù)據(jù)

本篇文章為大家展示了Android Studio中怎么綁定下拉框數(shù)據(jù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)專(zhuān)注服務(wù)器托管服務(wù)十載,提供服務(wù)器租用、虛擬空間、申請(qǐng)域名、云服務(wù)器、云主機(jī)租用、成都IDC機(jī)房托管、建站等服務(wù)

1、頁(yè)面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="掛號(hào)科室:"
 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;//掛號(hào)科室:

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ù)集名稱(chēng)為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, "該科室沒(méi)有醫(yī)生信息數(shù)據(jù)", Toast.LENGTH_SHORT).show();
    }

   } catch (JSONException e) {
    Toast.makeText(GV_Consultation.this, "數(shù)據(jù)請(qǐng)求失敗,請(qǐng)稍后重試", Toast.LENGTH_SHORT).show();
   }
  }

  @Override
  public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
   //請(qǐng)求失敗的回調(diào)處理
   Toast.makeText(GV_Consultation.this, "請(qǐng)鏈接網(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)容就是Android Studio中怎么綁定下拉框數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:AndroidStudio中怎么綁定下拉框數(shù)據(jù)
標(biāo)題網(wǎng)址:http://bm7419.com/article36/jjsjsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作商城網(wǎng)站、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、定制網(wǎng)站、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)