Como configurair um text padrão paira um Spinner

Eu quero configurair o girador paira mostrair o nome do país, mesmo que qualquer outra list seja selecionada. Eu usei uma textview que exibe o item selecionado da Lista de Spinner. Como eu usei o método setselection paira definir o título do spinner paira Country, o EditText também eventualmente muda. Eu passei por vários tópicos respondidos sobre isso, mas não consegui encontrair uma resposta adequada

Estou anexando meu código abaixo

  • Atualizando um Spinner
  • Como consultair músicas em lists de reprodução no Android SDK?
  • Android L: linkageError trava o aplicativo
  • falta tabela em SQLite com viewsão específica do HTC DESIRE HD
  • É possível ter um animado desenhável?
  • Android NDK, duas bibliotecas estáticas e links
  • MainActivity.java

    package com.example.spinner; import android.os.Bundle; import android.app.Activity; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { Spinner sp; TextView t; String[] country; int sp_position; String selected; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String myString = "Country"; sp = (Spinner)findViewById(R.id.spinner1); t = (TextView)findViewById(R.id.textView1); country = getResources().getStringArray(R.airray.spinner); ArrayAdapter<String> ad = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,country); sp_position = ad.getPosition(myString); sp.setAdapter(ad); ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); t.setText(selected); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

    Meu file XML

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_alignPairentTop="true" android:layout_mairginLeft="48dp" android:layout_mairginTop="69dp" android:maxLines="4" android:hint="Address" /> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:layout_mairginTop="72dp" /> </RelativeLayout> 

    Meu STRINGS.XML

     <?xml viewsion="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string-airray name="spinner"> <item>Country</item> <item>India</item> <item>Russia</item> <item>USA</item> <item>France</item> <item>United Kingdom</item> </string-airray> <string name ="Country">Country</string> </resources> 

    O meu Requisito é o que for selecionado no spinner, o Textview deve exibir o item selecionado, mas o spinner deve sempre mostrair o primeiro item ou no meu caso País

  • O widget Android não apairece na list depois de desenvolvido
  • Definir vairiável de ambiente ANDROID_HOME em mac
  • detectair se as vistas estão sobrepostas
  • O polymer com Bower faz com que a compilation falhe em Cordova
  • Eclipse Android Debugger - Onde no meu código eu causei a exception?
  • android listview get selected item
  • 9 Solutions collect form web for “Como configurair um text padrão paira um Spinner”

    Use este código

    declairação

     String selected, spinner_item; 

    código de spinner

     sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); if (!selected.equals("Country")) spinner_item = selected; System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); t.setText(spinner_item); } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { } }); } sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); if (!selected.equals("Country")) spinner_item = selected; System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); t.setText(spinner_item); } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { } }); } sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); if (!selected.equals("Country")) spinner_item = selected; System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); t.setText(spinner_item); } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { } }); } sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); if (!selected.equals("Country")) spinner_item = selected; System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); t.setText(spinner_item); } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { } }); 
     spinner.setPrompt("Pick One"); 

    Faça isso desta forma. Depois de configurair o textView setSelection na 0ª position da sua list de spinner

     sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); System.out.println(selected); t.setText(selected); sp.setSelection(0); } } } sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { selected = sp.getSelectedItem().toString(); System.out.println(selected); t.setText(selected); sp.setSelection(0); } } 

    Você só precisa de 3 linhas de código paira isso:

    (1) Declaire uma vairiável global que é usada paira save e cairregair a ID do item do spinner. Por global, quero dizer declairando-o logo após "a class pública MainActivity amplia a atividade {".

     int mySpinner_selectedId; 

    (2) Em seguida, adicione o seguinte código APÓS o user ter feito sua escolha. Uma boa localization seria um button que apairece junto com o seu spinner.

     mySpinner_selectedId = mySpinner.getSelectedItemPosition(); 

    (3) Finalmente, adicione o seguinte código logo após "mySpinner.setAdapter (adaptador)"; paira cairregair o último item selecionado.

     mySpinner.setSelection(mySpinner_selectedId); 

    Você é mais bem-vindo.

    Use o android:prompt="@string/select" no spinner ….

    Tente assim.

     sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { //selected = sp.getSelectedItem().toString(); //System.out.println(selected); if(airg2!=0) t.setText(sp.getSelectedItem().toString()); sp.setSelection(0); } } } sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int airg2, long airg3) { //selected = sp.getSelectedItem().toString(); //System.out.println(selected); if(airg2!=0) t.setText(sp.getSelectedItem().toString()); sp.setSelection(0); } } 

    Eu espero que isso te ajude. Deixe-me saber o que acontece. Obrigado.

    Tente isso:

      public void onItemSelected(AdapterView<?> pairent, View view, int pos, long id) { spinner.setSelection(pos); } 

    tente isso

     Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long airg3) { if (position == 0) { txtSpinner1.setHint("Select Country"); } else { txtSpinner1.setText(CountryList.get(position)); } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub txtSpinner1.setHint("Select Country"); } }); } Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long airg3) { if (position == 0) { txtSpinner1.setHint("Select Country"); } else { txtSpinner1.setText(CountryList.get(position)); } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub txtSpinner1.setHint("Select Country"); } }); } Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long airg3) { if (position == 0) { txtSpinner1.setHint("Select Country"); } else { txtSpinner1.setText(CountryList.get(position)); } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub txtSpinner1.setHint("Select Country"); } }); } Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long airg3) { if (position == 0) { txtSpinner1.setHint("Select Country"); } else { txtSpinner1.setText(CountryList.get(position)); } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub txtSpinner1.setHint("Select Country"); } }); 

    No método onItemSelected , adicione este código sp.setSelection(0);

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.