Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

343
Vistas
Android desplegable: llame a setOnItemSelectedListener solo cuando el usuario cambie su valor

Tengo un menú desplegable en el que configuro algunos valores y muestro, de forma predeterminada, el último valor que el usuario guardó de la preferencia compartida. Está funcionando bien. El problema es cuando inicio la actividad, para poner este valor de usuario guardado en la selección, de manera predeterminada, está llamando a setOnItemSelectedListener y mostrando un brindis para el usuario. No quiero ejecutar el código de bloque dentro de setOnItemSelectedListener esta vez, justo cuando el usuario cambia el valor desplegable. ¿Alguna idea de cómo solucionarlo?

 String defaultValue = pref02.getString("fonte", "Normal"); String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items); dropdown.setAdapter(adapter); int spinnerPosition = adapter.getPosition(defaultValue); dropdown.setSelection(spinnerPosition); //select the atual saved value. dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE); SharedPreferences.Editor editor = pref2.edit(); String drop = dropdown.getSelectedItem().toString(); editor.putString("fonte", drop); editor.apply(); Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parentView) { // your code here } });
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Solución: usando el siguiente código (puede consultar las líneas de comentarios para ver la guía)

 String defaultValue = pref02.getString("fonte", "Normal"); String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items); dropdown.setAdapter(adapter); int spinnerPosition = adapter.getPosition(defaultValue); dropdown.setSelection(spinnerPosition); //select the atual saved value. // Add this block of code dropdown.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { // Remove listener to make sure it doesn't called later. dropdown.removeOnLayoutChangeListener(this); // Set your listener here dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE); SharedPreferences.Editor editor = pref2.edit(); String drop = dropdown.getSelectedItem().toString(); editor.putString("fonte", drop); editor.apply(); Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parentView) { // your code here } }); } });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda