我有代碼從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();
}
});
}
如何設置可變Code
到OnSelectedItem
[編輯]
String selected = parent.getItemAtPosition(position).toString();
此代碼顯示與值「允許」的leaveType變量I的值需要該選擇的顯示從代碼的變量具有值「100」
你必須在轉碼的所有列表? – sushildlh
微調器中的代碼列表是什麼? – Aldan
你的微調值是什麼? – sushildlh