Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda