javaandroidandroid-arrayadapterandroid-spinnercustom-arrayadapter

Checking which ArrayAdapter Spinner currently uses


I have four ArrayAdapters that uses 4 different arrays

adapter1 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray( R.array.array1) );
adapter2 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array. array2));
adapter3 = new  ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array. array3));
adapter4 = new  ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array. array4));

An i have two spinners

spinnerFrom = findViewById(R.id.unit_From_spinner)
spinnertTo = findViewById(R.id.unit_To_spinner);

On clicked button , spinnerFrom and spinnertTo fills with adapter specified on code.

My main idea was write in project something like this:

if (spinnerFrom use adapter 1 or adapter 2 || spinnerTo use adapter 1 or adapter 2){
some code
}
else if (spinnerFrom use adapter 3 or adapter 4 || spinnerTO use adapter 3 or adapter 4){
some code
}

Question: How can I get the curently used adapter and specify its name (adapter1/adapter2/adapter3/adapter4) by spinnerFrom and spinnertTospinners and make if-else (or should i use swtich-case) structure?

Thank you!


Solution

  • A simple equality check should work:

    if (spinner.getAdapter() == adapterInstance) //etc
    

    Normally, you don't want to do this in Java, since it compares instances instead of values, but in this case, it should do what you need, which is compare instances.