Thursday, October 27, 2022

How to set selected item of Spinner by value, not by position

Solution 1

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
  
import java.util.Arrays;
  
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  
    // on below line we are creating variable for spinner.
    Spinner languageSpinner;
    // on below line we are creating a variable for our list of data to be displayed in spinner.
    String[] languages = {"C++", "Java", "Kotlin", "JavaScript", "Python", "PHP", "C#", "C"};
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing spinner with ids.
        languageSpinner = (Spinner) findViewById(R.id.idLanguageSpinner);
  
        // on below line we are initializing adapter for our spinner
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, languages);
  
        // on below line we are setting drop down view resource for our adapter.
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
        // on below line we are setting adapter for spinner.
        languageSpinner.setAdapter(adapter);
  
        // on below line we are adding click listener for our spinner
        languageSpinner.setOnItemSelectedListener(this);
  
        // on below line we are creating a variable to which we have to set our spinner item selected.
        String selection = "Python";
  
        // on below line we are getting the position of the item by the item name in our adapter.
        int spinnerPosition = adapter.getPosition(selection);
  
        // on below line we are setting selection for our spinner to spinner position.
        languageSpinner.setSelection(spinnerPosition);
    }
  
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // on below line we are displaying toast message for selected item
        Toast.makeText(MainActivity.this, "" + languages[position] + " Selected..", Toast.LENGTH_SHORT).show();
    }
  
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
  
    }
}


Solution 2


import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
   Spinner spinner;
   String[] FootBallPlayers = new String[]{"Lionel Messi", "Eden Hazard", "Cristiano Ronaldo", "Neymar Jr", "Gareth Bale", "David Bekham"};
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      spinner = findViewById(R.id.spinner);
      final List<String> plantsList = new ArrayList<>(Arrays.asList(FootBallPlayers));
      final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, plantsList);
      spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_value);
      spinner.setAdapter(spinnerArrayAdapter);
   }
   public void SelectSpinnerValue(View view) {
      spinner.setSelection(2);
   }
}



Solution 3

String compareValue = "some value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (compareValue != null) {
    int spinnerPosition = adapter.getPosition(compareValue);
    mSpinner.setSelection(spinnerPosition);
}

No comments:

Post a Comment