如何在Android上使用Retrofit2从Spinner检索2个值

时间:2017-10-10 04:27:10

标签: android json retrofit2

我有代码将数据从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
        }
    ]
}

到目前为止,我已尝试展示" PERMIT"来自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变量的值,其值为&#34; PERMIT&#34; 我需要此选项显示代码中的变量值&#34; 100&# 34;

2 个答案:

答案 0 :(得分: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) {
    }
});

答案 1 :(得分:1)

创建一个存储代码和leavetype的类,并将其添加到listSpinner列表

http.get()

在initSpinnerType()中进行更改,如下所示

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

public String getCode(){
return code;
}

现在访问代码thik this:

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