bks0088
bks0088

Reputation: 21

Android Spinner load with empty value (popup is shown before populating data)

I am using https://github.com/Chivorns/SmartMaterialSpinner Library for spinner. When i first click the spinner no adapter is attached to it or spinner items shows empty list.. like

enter image description here

but when i close it and again select it.. data are shown so I believe that .. due to time consuming data fetching .. spinner show empty dialog.. Now I need to handle that.. How can i only show spinner when data is populated or available.. I have tried async task and handler but not get working.. any hint would be appreciated.. Thank you

Edited ... I have call api on spinner onTouch event to populate data

 {
   private void ProvinceSpinnerCode(boolean IsEditCase) {

    if (IsEditCase) {
        //edit case condition
    } else {
        provinceSpinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mService = RetrofitManager.getApiClient().create(RetrofitInstance.class);
                Call<List<SelectModel>> provinceCall = mService.GetProvinces();
                provinceCall.enqueue(new Callback<List<SelectModel>>() {
                    @Override
                    public void onResponse(Call<List<SelectModel>> call, Response<List<SelectModel>> response) {
                        if (response.body().size() >= 0) {
                            provinceSpinner.setAdapter(new SpinnerAdapter(response.body(),AddCustomerActivity.this));

                        }
                    }

                    @Override
                    public void onFailure(Call<List<SelectModel>> call, Throwable t) {

                    }
                });
                return false;
            }
        });
    }

    provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(AddCustomerActivity.this, "Id is : " + id, Toast.LENGTH_SHORT).show();
            provinceId = (int) id;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

}

Upvotes: 0

Views: 129

Answers (1)

kelvin
kelvin

Reputation: 1538

You are making an API call inside on touch event which will be fired when you will touch the spinner so to solve the problem just do Api call out side that event (may be inside Oncreate).

Right now provinceSpinner has a touch listerner so when you are touching it to open for the first time it is Fetching data so when you are clicking it again the alert is showing data (the data you fetched when you clicked first time)

Upvotes: 1

Related Questions