Example usage for android.widget Spinner setAdapter

List of usage examples for android.widget Spinner setAdapter

Introduction

In this page you can find the example usage for android.widget Spinner setAdapter.

Prototype

@Override
public void setAdapter(SpinnerAdapter adapter) 

Source Link

Document

Sets the SpinnerAdapter used to provide the data which backs this Spinner.

Usage

From source file:org.runbuddy.tomahawk.ui.fragments.ChartsSelectorFragment.java

private void populateCountryCodeSpinner(final ScriptChartsProvider provider, final boolean isInitial) {
    if (mCountryCodePicker != null && provider != null) {
        provider.getCountryCodes().done(new DoneCallback<ScriptChartsCountryCodes>() {
            @Override/*from w  w w. ja  v  a  2s.c om*/
            public void onDone(final ScriptChartsCountryCodes result) {
                final ArrayList<String> countryCodes = new ArrayList<>();
                final ArrayList<String> displayedCountryCodes = new ArrayList<>();
                final ArrayList<String> displayedCountryCodeNames = new ArrayList<>();
                for (Pair<String, String> countryCode : result.codes) {
                    countryCodes.add(countryCode.second);
                    displayedCountryCodes.add(countryCode.second.toUpperCase());
                    displayedCountryCodeNames.add(countryCode.first);
                }
                int initialPosition = -1;
                if (isInitial) {
                    // Must be the first call of this method. So we should set the initially
                    // stored (or default) selection of the spinner
                    String storedCountryCode = ChartsSelectorFragment.this.getStoredCountryCode(provider);
                    if (storedCountryCode == null) {
                        storedCountryCode = result.defaultCode;
                    }
                    for (int i = 0; i < countryCodes.size(); i++) {
                        if (countryCodes.get(i).equalsIgnoreCase(storedCountryCode)) {
                            initialPosition = i;
                        }
                    }
                }
                Spinner mCountryCodePickerSpinner = (Spinner) mCountryCodePicker.getActionView();
                CountryCodeSpinnerAdapter adapter = new CountryCodeSpinnerAdapter(TomahawkApp.getContext(),
                        R.layout.spinner_textview_country_code, displayedCountryCodes,
                        displayedCountryCodeNames);
                adapter.setDropDownViewResource(R.layout.spinner_dropdown_textview_country_code);
                mCountryCodePickerSpinner.setAdapter(adapter);
                mCountryCodePickerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        String selectedCountryCode = countryCodes.get(position);
                        String storedCountryCode = ChartsSelectorFragment.this.getStoredCountryCode(provider);
                        if (storedCountryCode == null) {
                            storedCountryCode = result.defaultCode;
                        }
                        if (!storedCountryCode.equals(selectedCountryCode)) {
                            storeCountryCode(provider, selectedCountryCode);
                            mSelectedFragmentInfo.mBundle.putString(
                                    ChartsPagerFragment.CHARTSPROVIDER_COUNTRYCODE, selectedCountryCode);
                            // Refresh the currently shown ChartsPagerFragment
                            FragmentUtils.replace((TomahawkMainActivity) getActivity(),
                                    mSelectedFragmentInfo.mClass, mSelectedFragmentInfo.mBundle,
                                    R.id.content_frame);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
                if (initialPosition >= 0) {
                    mCountryCodePickerSpinner.setSelection(initialPosition);
                }
            }
        });
    }
}

From source file:com.micabytes.app.BaseFragment.java

@NonNull
protected Spinner setSpinner(int resId, int arrId, int spIt, int spDd) throws UIObjectNotFoundException {
    View root = getView();/*w w  w  .  j a  va  2 s .  c  o  m*/
    if (root == null)
        throw new UIObjectNotFoundException(COULD_NOT_FIND_THE_ROOT_VIEW);
    Spinner spinner = (Spinner) root.findViewById(resId);
    if (spinner == null)
        throw new UIObjectNotFoundException(COULD_NOT_FIND_RES_ID + resId + IN_FIND_VIEW_BY_ID);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
            arrId, spIt);
    adapter.setDropDownViewResource(spDd);
    spinner.setAdapter(adapter);
    return spinner;
}

From source file:com.example.android.popularmovies.app.MovieListingFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    movies = new ArrayList<MovieDetails>();

    movieAdapter = new MovieArrayAdapter(getActivity(), // The current context (this activity)
            R.layout.list_item_listing, // The name of the layout ID.
            R.id.list_item_listing_textview, // The ID of the textview to populate.
            movies);/*from  ww w  . j av a 2  s. c  o m*/

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    Spinner spinner = (Spinner) rootView.findViewById(R.id.sort_spinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.sort_options,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    FetchMovieListingTask movieListingTask = new FetchMovieListingTask();
    movieListingTask.execute(String.valueOf(spinner.getSelectedItemPosition()));

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            FetchMovieListingTask movieListingTask = new FetchMovieListingTask();
            movieListingTask.execute(String.valueOf(i));
        }

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

        }
    });

    // Get a reference to the ListView, and attach this adapter to it.
    GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_listing);
    gridView.setAdapter(movieAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // Log.i("MovieListingFragment", "Item clicked: " + view.toString() + " " + i + " " + l);
            getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null)
                    .replace(R.id.container, MovieDetailsFragment.newInstance(movies.get(i).id))
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
        }
    });

    return rootView;
}

From source file:com.nbos.phonebook.sync.authenticator.AuthenticatorActivity.java

/**
 * {@inheritDoc}/*from   w  ww.ja v a 2  s . c  o m*/
 */
@Override
public void onCreate(Bundle icicle) {
    Log.i(tag, "onCreate(" + icicle + ")");
    super.onCreate(icicle);
    mAccountManager = AccountManager.get(this);
    Log.i(tag, "loading data from Intent");
    final Intent intent = getIntent();
    mUsername = intent.getStringExtra(PARAM_USERNAME);
    mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
    mRequestNewAccount = mUsername == null;
    mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRMCREDENTIALS, false);

    Log.i(tag, "request new: " + mRequestNewAccount + " ,ConfirmCredentials: " + mConfirmCredentials);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.login_activity);
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.country_code_array,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
    setSpinnerCountry(spinner);
    mMessage = (TextView) findViewById(R.id.message);
    mUsernameEdit = (EditText) findViewById(R.id.username_edit);
    mPasswordEdit = (EditText) findViewById(R.id.password_edit);
    mPhoneEdit = (EditText) findViewById(R.id.phone_edit);
    getPhoneNumber(getApplicationContext());
    mUsernameEdit.setText(mUsername);
    mMessage.setText(getMessage());
}

From source file:com.vaquerosisd.projectmanager.NewTask.java

private void setSpinner(String spinnerFileName, ArrayAdapter<CharSequence> spinnerAdapter,
        Spinner spinnerObject, boolean sort) {
    String spinnerItems = fO.readFile(spinnerFileName);
    ArrayList<String> spinnerItemsArray = fO.convertToStringList(spinnerItems);
    if (sort)//from  w  w  w.j a  v a 2  s  .co  m
        Collections.sort(spinnerItemsArray.subList(0, spinnerItemsArray.size()));

    //Set the adapter for the status spinner 
    spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAdapter.addAll(spinnerItemsArray);
    spinnerObject.setAdapter(spinnerAdapter);
}

From source file:at.wada811.imageslider.DebugFragment.java

private void initInterpolator(Spinner interpolatorSpinner) {
    List<AnimationInterpolator> animationInterpolatorLists = Arrays.asList(AnimationInterpolator.values());
    ArrayList<Interpolator> tempAnimationInterpolators = new ArrayList<Interpolator>();
    for (AnimationInterpolator animationInterpolator : animationInterpolatorLists) {
        Interpolator interpolator = animationInterpolator.getInterpolator();
        tempAnimationInterpolators.add(interpolator);
    }/*w w w .j  av  a2  s.c o  m*/
    final ArrayList<Interpolator> animationInterpolators = new ArrayList<Interpolator>(
            tempAnimationInterpolators);
    interpolatorSpinner.setAdapter(new InterpolatorAdapter(getActivity(), animationInterpolators));
    interpolatorSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Interpolator interpolator = animationInterpolators.get(position);
            mCallback.setInterpolator(interpolator);
        }

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

        }
    });
}

From source file:com.app.blockydemo.content.bricks.SetVariableBrick.java

@Override
public View getView(final Context context, int brickId, BaseAdapter baseAdapter) {
    if (animationState) {
        return view;
    }//w w w  .  j a  va  2 s  .c om
    if (view == null) {
        alphaValue = 255;
    }

    view = View.inflate(context, R.layout.brick_set_variable, null);
    view = getViewWithAlpha(alphaValue);
    setCheckboxView(R.id.brick_set_variable_checkbox);

    final Brick brickInstance = this;
    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked = isChecked;
            adapter.handleCheck(brickInstance, isChecked);
        }
    });

    TextView prototypeText = (TextView) view.findViewById(R.id.brick_set_variable_prototype_view);
    TextView textField = (TextView) view.findViewById(R.id.brick_set_variable_edit_text);
    prototypeText.setVisibility(View.GONE);
    variableFormula.setTextFieldId(R.id.brick_set_variable_edit_text);
    variableFormula.refreshTextField(view);
    textField.setVisibility(View.VISIBLE);
    textField.setOnClickListener(this);

    Spinner variableSpinner = (Spinner) view.findViewById(R.id.set_variable_spinner);
    UserVariableAdapter userVariableAdapter = ProjectManager.getInstance().getCurrentProject()
            .getUserVariables().createUserVariableAdapter(context, sprite);
    UserVariableAdapterWrapper userVariableAdapterWrapper = new UserVariableAdapterWrapper(context,
            userVariableAdapter);
    userVariableAdapterWrapper.setItemLayout(android.R.layout.simple_spinner_item, android.R.id.text1);

    variableSpinner.setAdapter(userVariableAdapterWrapper);

    if (!(checkbox.getVisibility() == View.VISIBLE)) {
        variableSpinner.setClickable(true);
        variableSpinner.setEnabled(true);
    } else {
        variableSpinner.setClickable(false);
        variableSpinner.setFocusable(false);
    }

    setSpinnerSelection(variableSpinner, null);

    variableSpinner.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (((Spinner) view).getSelectedItemPosition() == 0
                        && ((Spinner) view).getAdapter().getCount() == 1) {
                    NewVariableDialog dialog = new NewVariableDialog((Spinner) view);
                    dialog.addVariableDialogListener(SetVariableBrick.this);
                    dialog.show(((FragmentActivity) view.getContext()).getSupportFragmentManager(),
                            NewVariableDialog.DIALOG_FRAGMENT_TAG);
                    return true;
                }
            }
            return false;
        }
    });
    variableSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0 && ((UserVariableAdapterWrapper) parent.getAdapter()).isTouchInDropDownView()) {
                NewVariableDialog dialog = new NewVariableDialog((Spinner) parent);
                dialog.addVariableDialogListener(SetVariableBrick.this);
                dialog.show(((FragmentActivity) view.getContext()).getSupportFragmentManager(),
                        NewVariableDialog.DIALOG_FRAGMENT_TAG);
            }
            ((UserVariableAdapterWrapper) parent.getAdapter()).resetIsTouchInDropDownView();
            userVariable = (UserVariable) parent.getItemAtPosition(position);
            adapterView = parent;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            userVariable = null;
        }
    });

    return view;
}

From source file:com.manning.androidhacks.hack017.CreateAccountAdapter.java

private void populateThirdForm(LinearLayout formLayout) {
    formLayout.addView(createTitle(mContext.getString(R.string.account_create_city_title)));
    EditText cityEditText = createEditText(mContext.getString(R.string.account_create_city_hint),
            InputType.TYPE_CLASS_TEXT, EditorInfo.IME_ACTION_DONE, false, CITY_KEY);

    if (mFormData.get(CITY_KEY) != null) {
        cityEditText.setText(mFormData.get(CITY_KEY));
    }//from  w  w  w  . j a va  2 s  .c  o m

    formLayout.addView(cityEditText);
    formLayout.addView(createErrorView(CITY_KEY));

    formLayout.addView(createTitle(mContext.getString(R.string.account_create_country_title)));

    Spinner spinner = new Spinner(mContext);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.bottomMargin = 17;
    spinner.setLayoutParams(params);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
            android.R.layout.simple_spinner_item, Countries.COUNTRIES);
    spinner.setAdapter(adapter);
    spinner.setPrompt(mContext.getString(R.string.account_create_country_spinner_prompt));
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

            mFormData.put(COUNTRY_KEY, Countries.COUNTRY_CODES[pos]);
        }

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

    if (mFormData.get(COUNTRY_KEY) != null) {
        List<String> array = Arrays.asList(Countries.COUNTRY_CODES);
        spinner.setSelection(array.indexOf(mFormData.get(COUNTRY_KEY)));
    }

    formLayout.addView(spinner);
}

From source file:com.app.blockydemo.content.bricks.ChangeVariableBrick.java

@Override
public View getView(final Context context, int brickId, BaseAdapter baseAdapter) {
    if (animationState) {
        return view;
    }/*w w  w.j  a  va  2s  .  c o  m*/

    view = View.inflate(context, R.layout.brick_change_variable_by, null);
    view = getViewWithAlpha(alphaValue);
    setCheckboxView(R.id.brick_change_variable_checkbox);
    final Brick brickInstance = this;

    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked = isChecked;
            adapter.handleCheck(brickInstance, isChecked);
        }
    });

    TextView prototypeText = (TextView) view.findViewById(R.id.brick_change_variable_prototype_view);
    TextView textField = (TextView) view.findViewById(R.id.brick_change_variable_edit_text);
    prototypeText.setVisibility(View.GONE);
    variableFormula.setTextFieldId(R.id.brick_change_variable_edit_text);
    variableFormula.refreshTextField(view);
    textField.setVisibility(View.VISIBLE);
    textField.setOnClickListener(this);

    Spinner variableSpinner = (Spinner) view.findViewById(R.id.change_variable_spinner);
    UserVariableAdapter userVariableAdapter = ProjectManager.getInstance().getCurrentProject()
            .getUserVariables().createUserVariableAdapter(context, sprite);
    UserVariableAdapterWrapper userVariableAdapterWrapper = new UserVariableAdapterWrapper(context,
            userVariableAdapter);
    userVariableAdapterWrapper.setItemLayout(android.R.layout.simple_spinner_item, android.R.id.text1);

    variableSpinner.setAdapter(userVariableAdapterWrapper);

    if (!(checkbox.getVisibility() == View.VISIBLE)) {
        variableSpinner.setClickable(true);
        variableSpinner.setEnabled(true);
    } else {
        variableSpinner.setClickable(false);
        variableSpinner.setFocusable(false);
    }

    setSpinnerSelection(variableSpinner, null);

    variableSpinner.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (((Spinner) view).getSelectedItemPosition() == 0
                        && ((Spinner) view).getAdapter().getCount() == 1) {
                    NewVariableDialog dialog = new NewVariableDialog((Spinner) view);
                    dialog.addVariableDialogListener(ChangeVariableBrick.this);
                    dialog.show(((FragmentActivity) view.getContext()).getSupportFragmentManager(),
                            NewVariableDialog.DIALOG_FRAGMENT_TAG);
                    return true;
                }
            }
            return false;
        }
    });

    variableSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0 && ((UserVariableAdapterWrapper) parent.getAdapter()).isTouchInDropDownView()) {
                NewVariableDialog dialog = new NewVariableDialog((Spinner) parent);
                dialog.addVariableDialogListener(ChangeVariableBrick.this);
                dialog.show(((FragmentActivity) view.getContext()).getSupportFragmentManager(),
                        NewVariableDialog.DIALOG_FRAGMENT_TAG);
            }
            ((UserVariableAdapterWrapper) parent.getAdapter()).resetIsTouchInDropDownView();
            userVariable = (UserVariable) parent.getItemAtPosition(position);
            adapterView = parent;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            userVariable = null;
        }
    });

    return view;
}

From source file:eu.operando.operandoapp.filters.response.ResponseFiltersActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(MainContext.INSTANCE.getSettings().getThemeStyle().themeAppCompatStyle());
    updateFiltersList();/*www.j a  v a2  s.  c  o  m*/
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filters_content);
    ButterKnife.inject(this);

    userResponseFiltersAdapter = new UserResponseFiltersAdapter();
    externalResponseFiltersAdapter = new ExternalResponseFiltersAdapter();

    recyclerView = new RecyclerView(this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    //recyclerView.setAdapter(userResponseFiltersAdapter);
    //recyclerViewHolder.addView(recyclerView);

    //Setup Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        View spinnerContainer = LayoutInflater.from(this).inflate(R.layout.toolbar_spinner, toolbar, false);
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        toolbar.addView(spinnerContainer, lp);

        ResponseFiltersSpinnerAdapter spinnerAdapter = new ResponseFiltersSpinnerAdapter();

        Spinner spinner = (Spinner) spinnerContainer.findViewById(R.id.toolbar_spinner);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                viewSelected = position;
                recyclerViewHolder.removeAllViews();
                //recyclerViewHolder.invalidate();
                switch (position) {
                case 0:
                    recyclerView.setAdapter(userResponseFiltersAdapter);
                    break;
                case 1:
                    recyclerView.setAdapter(externalResponseFiltersAdapter);
                    break;
                default:
                    recyclerView.setAdapter(userResponseFiltersAdapter);
                    break;
                }
                recyclerViewHolder.addView(recyclerView);
                inValidateSelections();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }
}