I want to show a district list in a spinner, and i am getting that data through an API. Here is an example of the response.
{
"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"
}
After that, I created 2 schemapojo class for response and dataList as well.
Then, I am calling this into a class but its not working!! I don't have any idea how to call an API and set the response data into a spinner. Please help
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) {
}
});
}
Here in code district_List variable is for a second model class where all district List is available.
I tried a different process from youtube but none of that worked !!
CodePudding user response:
Change code
districts = new ArrayAdapter<String>(this,R.layout.dist_list,district_list);
To this
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);
CodePudding user response:
You can also try like this
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);
And here create a model like this
Example Model Class
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; } }
District Model Class
public class District { @SerializedName("district") @Expose private String district; public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } }
I just hope your problem is solved using this