I am using retrofit2 and here i am retrieving the data object from server which gives the list of objects present inside, how to know the size of the list. the list will provide objects so through those objects i can retrieve clients data in arrays.
{
"data": [{
"ClientName": "Shoib hasan",
"CompanyName": "097676",
"CityName": "Badin",
"ContactNo": "sfgt",
"TotalBalance": "11000.00",
"AddedOn": "2017-07-18"
}, {
"ClientName": "Musakan Chappal",
"CompanyName": "Muskan Chappal",
"CityName": "Badin",
"ContactNo": "",
"TotalBalance": "15000.00",
"AddedOn": "2017-07-18"
}]
}
From Class Data i retrieve data object
Data data = response.body;
now get list size present inside data object
public class Datum {
@SerializedName("ClientName")
@Expose
private String clientName;
@SerializedName("CompanyName")
@Expose
private String companyName;
@SerializedName("CityName")
@Expose
private String cityName;
@SerializedName("ContactNo")
@Expose
private String contactNo;
@SerializedName("TotalBalance")
@Expose
private String totalBalance;
@SerializedName("AddedOn")
@Expose
private String addedOn;
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getTotalBalance() {
return totalBalance;
}
public void setTotalBalance(String totalBalance) {
this.totalBalance = totalBalance;
}
public String getAddedOn() {
return addedOn;
}
public void setAddedOn(String addedOn) {
this.addedOn = addedOn;
}
}
this is Example class
public class Example {
@SerializedName("data")
@Expose
private List<Datum> data ;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
}
this is method in activity
public void fetch_information(String CityId) {
this.CityID = CityId;
ApiInterface = ApiClient.getApiClient().create(Api.class);
Call<Example> call = ApiInterface.GetClients(CityID);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
data = new Example();
data.setData(response.body().getData());
Integer clientlist_size = response.body().getData().size();
prepareClientsData();
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
}
public void prepareClientsData() {
clients = new String[data.getData().size()];
for(int i = 0; i<data.getData().size();i++){
clients[i] = data.getData().get(i).getClientName();
}
client_adapter = new ArrayAdapter<String>(EzKhataActivity.this, android.R.layout.simple_list_item_1, clients);
client_adapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
client_spinner.setAdapter(client_adapter);
this is i am getting