如何使用微调器翻新来检索2个值

时间:2019-12-12 09:49:48

标签: java android json retrofit2

我有这样的代码JSON,我想当我选择Spinner selecteditem的值是Baru时,它将显示在Toast Baru的ID中。

JSON:

1

到目前为止,我已经尝试从JSON显示Baru的ID:

spinnerProvinsi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        int IDProvinces = getProvincesIDList.get(i - 1);
        Toast.makeText(getContext(), "ID Province : " + IDProvinces, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});

有响应:

private void fetchProvinces(int country_id) {
    Call<JsonObject> getProvinces = baseApiService.getProvinces(country_id);
    getProvinces.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            if (!response.isSuccessful()) {
                Log.e(TAG, "Request Error : " + response.code());
            } else {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    modelGetProvincesList = new ArrayList<>();
                    getProvincesIDList = new ArrayList<>();

                    for (int a = 0; a < jsonArray.length(); a++) {
                        objectProvinces = jsonArray.getJSONObject(a);
                        getIDProvinces = objectProvinces.getInt("id");
                        modelGetProvincesList.add(objectProvinces.getString("name"));
                        getProvincesIDList.add(getIDProvinces);
                    }

                    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, modelGetProvincesList);
                    spinnerProvinsi.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            Log.e(TAG, "onFailure : " + t.getMessage());
        }
    });
}

当我调试时,错误出现在logcat中:

java.lang.ArrayIndexOutOfBoundsException: length=49; index=-1
    at java.util.ArrayList.get(ArrayList.java:439)
    at com.product.shopciety.ui.menu.menu_signin.menu.MenuTambahAlamatFragment$2$1.onItemSelected(MenuTambahAlamatFragment.java:128)

1 个答案:

答案 0 :(得分:1)

不提供i-1。索引也从0开始,但是当您尝试选择baru时,它的位置为0 - 1 = -1。因此,只需提供i,它是ArrayList中baru的索引。

spinnerProvinsi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        int IDProvinces = getProvincesIDList.get(i);
        Toast.makeText(getContext(), "ID Province : " + IDProvinces, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});