gpt4 book ai didi

android - 如何在 Android 上使用 Retrofit2 从微调器中检索 2 个值

转载 作者:行者123 更新时间:2023-11-29 19:07:28 24 4
gpt4 key购买 nike

我有代码可以从 JSON 调用数据至 Spinner , 并在 Toast 中显示数据当数据被选中时,但我想从JSON中获取第二个数据,例如类型我选择 "PERMIT" 它会在 Toast 中显示代码 "100" (这将是我保存到 TextView )

JSON:

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

到目前为止,我已经尝试显示来自 JSON 的“PERMIT”结果

    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();

此代码显示值 "PERMIT" 的 leaveType 变量的值我需要这个选择显示代码中的变量值 "100"

最佳答案

在您的回复中:

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) {
}
});

关于android - 如何在 Android 上使用 Retrofit2 从微调器中检索 2 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46658617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com