skip to Main Content

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


  1. First create the ArrayList of retrieve data from api.

    ArrayList<String> arrayList = new ArrayList<String>();
    
    arrayList.add("Item1");
    arrayList.add("Item2");
    arrayList.add("Item3");
    

    And create a ArrayAdapter of the data.

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item, arrayList);
    spinner.setAdapter(arrayAdapter);
    
    Login or Signup to reply.
  2. Try this code :

        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) {
                    ArrayList<String> arrayList = new ArrayList<String>();
                    for (int i = 0; i < response.body().getData().getStars().size(); i++) {
                        arrayList.add(response.body().getData().getStars().get(i).getName());
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_hint_item, arrayList);
                    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
                    SerialReview_CastSpinner.setAdapter(adapter);
    
                    SerialReview_CastProgress.hide();
                }
            }
    
            @Override
            public void onFailure(Call<SeriesDetailResponse> call, Throwable t) {
            }
        });
    
    Login or Signup to reply.
  3. You should only be adding to the adapter in the callback, not creating one as it is less efficient in object creation.

    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();
        }
    

    If you had a List<String> with ArrayAdapter<String> or used an ArrayAdapter<Star>, you could shorten that loop with adapter.addAll()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search