2017-10-10 72 views
0

我有代碼從JSON數據呼叫Spinner,並顯示數據Toast當數據選擇,但我想從JSON採取第二種數據,檢索2倍的值與Retrofit2例如I型選擇「許可證」它會顯示代碼「100」 吐司(這將是我保存到TextView如何從微調在Android

JSON:

{ 
    "total_results": "3", 
    "value": 1, 
    "status": true, 
    "result": [ 
     { 
      "GroupCode": "I", 
      "Code": "100", 
      "Type": "PERMIT", 
      "MaxTotalReq": "0", 
      "MinRequest": null 
     } 
    ] 
} 

到目前爲止,我已經試過,顯示 「許可證」 從JSON

leaveType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      String selected = parent.getItemAtPosition(position).toString(); 
      Toast.makeText(context, "Choose " + selected, Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
     } 
    }); 

結果與響應

private void initSpinnerType(){ 
     RetrofitInterface api = RetrofitClient.getClient().create(RetrofitInterface.class); 
     Call<LeaveTypeResponse> call = api.getDataPermit(); 
     call.enqueue(new Callback<LeaveTypeResponse>() { 
      @Override 
      public void onResponse(Call<LeaveTypeResponse> call, Response<LeaveTypeResponse> response) { 
       if (response.isSuccessful()) { 
        List<LeaveType> leaveTypeItems = response.body().getResult(); 
        for (int i = 0; i < leaveTypeItems.size(); i++){ 
         String Code = leaveTypeItems.get(i).getCode(); // I want to show this when Selected 
         String leaveType = leaveTypeItems.get(i).getType(); 
         listSpinner.add(leaveType); 
        } 
        listSpinner.add(0, "- SELECT TYPE -"); 

        adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, listSpinner); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        leaveType.setAdapter(adapter); 

       } else { 
        Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      @Override 
      public void onFailure(Call<LeaveTypeResponse> call, Throwable t) { 
       t.printStackTrace(); 
       Toast.makeText(context, "CAN'T CONNECT", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

如何設置可變CodeOnSelectedItem

[編輯]

String selected = parent.getItemAtPosition(position).toString();

此代碼顯示與值「允許」的leaveType變量I的值需要該選擇的顯示從代碼的變量具有值「100」

+0

你必須在轉碼的所有列表? – sushildlh

+0

微調器中的代碼列表是什麼? – Aldan

+0

你的微調值是什麼? – sushildlh

回答

1

在回信:

private void initSpinnerType(){ 
    RetrofitInterface api = RetrofitClient.getClient().create(RetrofitInterface.class); 
    Call<LeaveTypeResponse> call = api.getDataPermit(); 
    call.enqueue(new Callback<LeaveTypeResponse>() { 
     @Override 
     public void onResponse(Call<LeaveTypeResponse> call, Response<LeaveTypeResponse> response) { 
      if (response.isSuccessful()) { 
       List<LeaveType> leaveTypeItems = response.body().getResult(); 
       for (int i = 0; i < leaveTypeItems.size(); i++){ 
        String Code = leaveTypeItems.get(i).getCode(); // I want to show this when Selected 
        String leaveType = leaveTypeItems.get(i).getType(); 
        listSpinner.add(leaveType); 
        // Add your code into an separate ArrayList 
        listCode.add(code); 
       } 
       listSpinner.add(0, "- SELECT TYPE -"); 

       adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, listSpinner); 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       leaveType.setAdapter(adapter); 

      } else { 
       Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     @Override 
     public void onFailure(Call<LeaveTypeResponse> call, Throwable t) { 
      t.printStackTrace(); 
      Toast.makeText(context, "CAN'T CONNECT", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

監聽:

leaveType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // position -1 because in listspinner we add an extra String at 0 index 
     String selectedCode=listCode.get(--position); 
     String selected = parent.getItemAtPosition(position).toString(); 
     Toast.makeText(context, "Choose " + selectedCode, Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 
}); 
+0

嗨,謝謝你的答案。但是當Activity第一次自動打開時,默認的微調是**(「 - SELECT TYPE - 」)** **(「 - SELECT TYPE - 」)**如果打開「Toast」內容爲「100」在Index爲1時,但當索引爲0 Toast時,問題出現在String selectedCode = listCode.get( - position);如果位置是0,則' – Aldan

+0

設置條件,然後不顯示吐司。 – ved

+0

是的,這是真的,但直接到** 101 **的代碼,請參閱我上面的'JSON POJO'。所以'代碼** ** 100 **在通過 – Aldan

1

創建一個存儲代碼和leavetype類並將其添加到列表listSpinner

class Data{ 
    String code; 
    String leaveType; 
    public Data(String code,String leaveType){ 
     this.code=code; 
this.leavetype=leavetype; 
} 

public String getCode(){ 
return code; 
} 

在initSpinnerType(做出改變),如下

ArrayList<String> codeList=new ArrayList<String>(); 


ArrayList<String> leaveList=new ArrayList<String>(); 

private void initSpinnerType(){ 
     RetrofitInterface api = RetrofitClient.getClient().create(RetrofitInterface.class); 
     Call<LeaveTypeResponse> call = api.getDataPermit(); 
     call.enqueue(new Callback<LeaveTypeResponse>() { 
      @Override 
      public void onResponse(Call<LeaveTypeResponse> call, Response<LeaveTypeResponse> response) { 
       if (response.isSuccessful()) { 
        List<LeaveType> leaveTypeItems = response.body().getResult(); 
codeList.clear(); 
leaveList.clear(); 
        for (int i = 0; i < leaveTypeItems.size(); i++){ 

        codeList.add(leaveTypeItems.get(i).getCode()); 
leaveList.add(leaveTypeItems.get(i).getType()); 
         listSpinner.add(leaveList); 
        } 
        listSpinner.add(0, "- SELECT TYPE -"); 

        adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, listSpinner); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        leaveType.setAdapter(adapter); 

       } else { 
        Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      @Override 
      public void onFailure(Call<LeaveTypeResponse> call, Throwable t) { 
       t.printStackTrace(); 
       Toast.makeText(context, "CAN'T CONNECT", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

現在訪問代碼THIK這樣的:

leaveType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     String selected = parent.getItemAtPosition(position); 
String code=codeList.get(position); 
     Toast.makeText(context, "Choose " + code, Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 
}); 
+0

它不適用於我 – Aldan

+0

我可以得到您使用此代碼得到的錯誤 –

+0

讓它做出一些改變,請做同樣的事情,它一定會工作 –