在我的应用程序中,我想从服务器向Spinner显示一些数据
对于Spinner我使用这个库:https://github.com/ganfra/MaterialSpinner
我的Json:
2017-05-14
要连接到服务器,我使用"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": ""
}
]
库并在代码下面写
Retrofit
但我不知道如何将星级类中的名称添加到ArrayAdapter中以便在Spinner中设置。
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) {
}
});
答案 0 :(得分:0)
首先从api创建ArrayList
检索数据。
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Item1");
arrayList.add("Item2");
arrayList.add("Item3");
并创建ArrayAdapter
数据。
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item, arrayList);
spinner.setAdapter(arrayAdapter);
答案 1 :(得分:0)
您应该只在回调中添加适配器,而不是创建一个,因为它在对象创建方面效率较低。
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
call.enqueue(new Callback<SeriesDetailResponse>() {
@Override
public void onResponse(Call<SeriesDetailResponse> call, Response<SeriesDetailResponse> response) {
SeriesDetailResponse body = response.body();
List<Star> stars = body.getStars();
adapter.clear();
for (Star star : stars) {
adapter.add(star.toString());
}
progress.hide();
}
如果您有List<String>
ArrayAdapter<String>
或使用ArrayAdapter<Star>
,则可以使用adapter.addAll()
缩短该循环
答案 2 :(得分:-1)
试试这段代码:
ClassCastException