如何从API调用数据到旋转器?



我想在旋转器中显示一个地区列表,并且我通过API获得该数据。下面是一个响应的例子。

{
"districts": [
{
"district": "ANANTNAG"
},
{
"district": "BADGAM"
},
{
"district": "BANDIPORA"
},
{
"district": "BARAMULLA"
},
{
"district": "DODA"
},
{
"district": "GANDERBAL"
},
{
"district": "JAMMU"
},
{
"district": "KARGIL"
},
{
"district": "KATHUA"
},
{
"district": "KISHTWAR"
},
{
"district": "KULGAM"
},
{
"district": "KUPWARA"
},
{
"district": "LEH LADAKH"
},
{
"district": "POONCH"
},
{
"district": "PULWAMA"
},
{
"district": "RAJAURI"
},
{
"district": "RAMBAN"
},
{
"district": "REASI"
},
{
"district": "SAMBA"
},
{
"district": "SHOPIAN"
},
{
"district": "SRINAGAR"
},
{
"district": "UDHAMPUR"
},
{
"district": "JAMMU AND KASHMIR"
}
],
"Request_type": "districts",
"responseCode": "Success"
}

之后,我为response和dataList创建了2个schemapojo类。

然后,我把它调用到一个类中,但它不起作用!!我不知道如何调用API并将响应数据设置为旋转器。请帮助

private void getdistricts(String state) {
JSONObject mJobj = new JSONObject();
try {
mJobj.put("state", state);
mJobj.put(REQUEST, "districts");
} catch (JSONException e) {
e.printStackTrace();
}

Call<districtresponse> call = RetrofitClient.getInstance().getApi().getDistrict(mJobj);
call.enqueue(new Callback<districtresponse>() {
@Override
public void onResponse(Call<districtresponse> call, Response<districtresponse> response) {
String status = response.body().getResponseCode();
if (status.equalsIgnoreCase("Success")){
district_list = response.body().getDistricts();
districts = new ArrayAdapter<String>(this,R.layout.dist_list,district_list);
}
}
@Override
public void onFailure(Call<districtresponse> call, Throwable t) {

}
});
}

在代码中,distrit_list变量用于第二个模型类,其中所有地区列表都可用。

我尝试了一个不同的过程,但没有一个工作!!

更改代码

districts = new ArrayAdapter<String>(this,R.layout.dist_list,district_list);

ArrayAdapter adapter
= new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
districts);
// set simple layout resource file
// for each item of spinner
adapter.setDropDownViewResource(
android.R.layout
.simple_spinner_dropdown_item);
// Set the ArrayAdapter (ad) data on the
// Spinner which binds data to spinner
spino.setAdapter(adapter);

您也可以这样尝试

ArrayList<String> stringArrayList = null; //Create A global Variable
Call<Example> call = RetrofitClient.getInstance().getApi().getDistrict();
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
if (response.code() == 200) {
ArrayList<District> districts = (ArrayList<District>) response.body().getDistricts();
for (int i = 0; i < districts.size(); i++) {
stringArrayList.add(districts.get(i).getDistrict());
}
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {

}
});
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.dist_list,district_list,stringArrayList);
arrayAdapter.setDropDownViewResource(R.layout.dist_list,district_list);
yourSpinner.setAdapter(arrayAdapter);

在这里创建一个像这样的模型

  1. 示例模型类

    public class Example {
    @SerializedName("districts")
    @Expose
    private List<District> districts = null;
    @SerializedName("Request_type")
    @Expose
    private String requestType;
    @SerializedName("responseCode")
    @Expose
    private String responseCode;
    public List<District> getDistricts() {
    return districts;
    }
    public void setDistricts(List<District> districts) {
    this.districts = districts;
    }
    public String getRequestType() {
    return requestType;
    }
    public void setRequestType(String requestType) {
    this.requestType = requestType;
    }
    public String getResponseCode() {
    return responseCode;
    }
    public void setResponseCode(String responseCode) {
    this.responseCode = responseCode;
    }
    }
    
  2. 地区模范班

    public class District {
    @SerializedName("district")
    @Expose
    private String district;
    public String getDistrict() {
    return district;
    }
    public void setDistrict(String district) {
    this.district = district;
    }
    }
    

我只是希望你的问题解决了

相关内容

  • 没有找到相关文章

最新更新