public class

AbsSpinnerBindingAdapter

extends java.lang.Object

 java.lang.Object

↳androidx.databinding.adapters.AbsSpinnerBindingAdapter

Gradle dependencies

compile group: 'androidx.databinding', name: 'adapters', version: '3.2.0-alpha11'

  • groupId: androidx.databinding
  • artifactId: adapters
  • version: 3.2.0-alpha11

Artifact androidx.databinding:adapters:3.2.0-alpha11 it located at Google repository (https://maven.google.com/)

Androidx class mapping:

androidx.databinding.adapters.AbsSpinnerBindingAdapter android.databinding.adapters.AbsSpinnerBindingAdapter

Summary

Constructors
publicAbsSpinnerBindingAdapter()

Methods
public static voidsetEntries(AbsSpinner view, java.util.List<java.lang.Object> entries)

public static voidsetEntries(AbsSpinner view, java.lang.CharSequence entries[])

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public AbsSpinnerBindingAdapter()

Methods

public static void setEntries(AbsSpinner view, java.lang.CharSequence entries[])

public static void setEntries(AbsSpinner view, java.util.List<java.lang.Object> entries)

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.databinding.adapters;

import androidx.databinding.BindingAdapter;
import androidx.annotation.RestrictTo;
import android.widget.AbsSpinner;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

import java.util.List;

/**
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
@SuppressWarnings("unused")
public class AbsSpinnerBindingAdapter {

    @BindingAdapter({"android:entries"})
    public static <T extends CharSequence> void setEntries(AbsSpinner view, T[] entries) {
        if (entries != null) {
            SpinnerAdapter oldAdapter = view.getAdapter();
            boolean changed = true;
            if (oldAdapter != null && oldAdapter.getCount() == entries.length) {
                changed = false;
                for (int i = 0; i < entries.length; i++) {
                    if (!entries[i].equals(oldAdapter.getItem(i))) {
                        changed = true;
                        break;
                    }
                }
            }
            if (changed) {
                ArrayAdapter<CharSequence> adapter =
                        new ArrayAdapter<CharSequence>(view.getContext(),
                                android.R.layout.simple_spinner_item, entries);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                view.setAdapter(adapter);
            }
        } else {
            view.setAdapter(null);
        }
    }

    @BindingAdapter({"android:entries"})
    @SuppressWarnings("unchecked")
    public static <T> void setEntries(AbsSpinner view, List<T> entries) {
        if (entries != null) {
            SpinnerAdapter oldAdapter = view.getAdapter();
            if (oldAdapter instanceof ObservableListAdapter) {
                //noinspection unchecked
                ((ObservableListAdapter) oldAdapter).setList(entries);
            } else {
                view.setAdapter(new ObservableListAdapter<>(view.getContext(), entries,
                        android.R.layout.simple_spinner_item,
                        android.R.layout.simple_spinner_dropdown_item, 0));
            }
        } else {
            view.setAdapter(null);
        }
    }
}