In my application I want show some data from server into Spinner.
For Spinner I use this library : https://github.com/ganfra/MaterialSpinner
My Json :
"stars": [
{
"id": 105076,
"name": "Eleanor Tomlinson",
"character": null,
"imageUrl": "http://example.com/cpanel/uploads/Celebrities/105076/thumb2-SK4MPEGW09.jpg",
"userReview": ""
},
{
"id": 10127,
"name": "Aidan Turner",
"character": null,
"imageUrl": "http://example.com/cpanel/uploads/Celebrities/10127/thumb2-KL9G4TNGW2.jpg",
"userReview": ""
},
{
"id": 21340,
"name": "Gabriella Wilde",
"character": null,
"imageUrl": "http://example.com/cpanel/uploads/Celebrities/21340/thumb2-UD5HJ2EICN.jpg",
"userReview": ""
}
]
For connect to server I use Retrofit
Library and write below codes :
final SerialDetailSendData sendData = new SerialDetailSendData();
sendData.setSeriesID(serialID);
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<SeriesDetailResponse> call = api.getSeriesDetail(sendData);
call.enqueue(new Callback<SeriesDetailResponse>() {
@Override
public void onResponse(Call<SeriesDetailResponse> call, Response<SeriesDetailResponse> response) {
if (response.body().getData() != null) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_hint_item,
???);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
SerialReview_CastSpinner.setAdapter(adapter);
SerialReview_CastProgress.hide();
}
}
@Override
public void onFailure(Call<SeriesDetailResponse> call, Throwable t) {
}
});
But I don’t know how can I add names from stars class into ArrayAdapter for set in Spinner.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_hint_item,
???);
3
Answers
First create the
ArrayList
of retrieve data from api.And create a
ArrayAdapter
of the data.Try this code :
You should only be adding to the adapter in the callback, not creating one as it is less efficient in object creation.
If you had a
List<String>
withArrayAdapter<String>
or used anArrayAdapter<Star>
, you could shorten that loop withadapter.addAll()