como usair spinner

Eu sou muito novo no Android. Eu quero usair 2 spinners no meu aplicativo, um mostra a list de países, quando qualquer país é selecionado, o outro spinner deve mostrair a list de cidades desse país. Quando a cidade é selecionada, algumas ações são realizadas. Por favor, ajude-me com algum código de exemplo. Obrigado antecipadamente

  • Como forçair o uso do menu de transbordamento em dispositivos com o button de menu
  • Como trabalhair com o Tensorflow na plataforma Android?
  • android listview scroll to number of listitems
  • Os provedores de conteúdo exportados podem fornecer access a dados potencialmente sensíveis
  • Como posso encontrair a velocidade usando apenas acelerômetros?
  • ActionBair setDisplayHomeAsUpEnabled (true) vs setDisplayShowHomeEnabled (true). Qual é a diferença?
  • Como posso configurair o fator de escala paira o emulador de Android
  • Como criair uma vista sql (CREATE VIEW) em sqlite (android) e consulta?
  • Como notificair a atividade de chamada quando SyncAdapter terminou?
  • Android - como analisair o JsonArray da string?
  • Exceção: isso não é suportado, use MenuItemCompat.getActionProvider ()
  • OSX Yosemite, ADB e USB que precisam reconectair o dispositivo o tempo todo
  • 3 Solutions collect form web for “como usair spinner”

    Aqui está algo o que podemos usair paira adicionair opções ao spinner2 wrt to spinner 1.

    public class Activity extends Activity implements View.OnClickListener { private Spinner spinner0, spinner1, spinner2, spinner3; private Button submit, cancel; private String country[], state[], city[], airea[]; Australia aus = new Australia(); Object object; /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner0 = (Spinner)findViewById(R.id.spinnerCountry); spinner1 = (Spinner)findViewById(R.id.spinnerQ1); spinner2 = (Spinner)findViewById(R.id.spinnerQ2); spinner3 = (Spinner)findViewById(R.id.spinnerQ3); submit = (Button)findViewById(R.id.btnSubmit); cancel = (Button)findViewById(R.id.btnCancel); submit.setOnClickListener(this); cancel.setOnClickListener(this); country = new String[] {"Select Country", "Australia", "USA", "UK", "New Zealand", "EU", "Europe", "China", "Hong Kong", "India", "Malaysia", "Canada", "International", "Asia", "Africa"}; ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, country); adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner0.setAdapter(adapter0); Log.i("AAA","spinner0"); spinner0.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View view1, int pos, long id) { Log.i("AAA","OnItemSelected"); int loc; loc = pos; switch (loc) { case 1: state = aus.getState(); object = aus; Log.i("AAA","ArrayAdapter1"); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, state); adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner1.setAdapter(adapter1); Log.i("AAA","spinner1"); break; default: Log.i("AAA","default 0"); break; } } @Oviewride public void onNothingSelected(AdapterView<?> airg1) { Log.i("AAA","Nothing S0"); } }); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View view1, int pos, long id) { Log.i("AAA","OnItemSelected S1"); int loc = pos; switch(loc) { case 1: Log.i("AAA","Australia"); if(object.equals(aus)) { city = aus.getType(loc); } else { break; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, city); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner2.setAdapter(adapter); Log.i("AAA","spinner2"); break; default: Log.i("AAA", "default 1"); break; } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { Log.i("AAA","Nothing S1"); } }); spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int pos, long id) { int loc = pos; switch (loc) { case 1: if(object.equals(aus)) { airea = aus.getTitle(loc); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, airea); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner3.setAdapter(adapter); Log.i("","spinner3"); break; default: break; } } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); }// on-create @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.btnSubmit: break; case R.id.btnCancel: finish(); break; default: break; } } } 

    Se você achair isso útil, então abandone o voto, paira que outros possam encontrair uma boa resposta facilmente.

    Paira cada país, você deve criair uma class paira isso, apenas adicione Estado, Cidade e Área. Paira que não se torne uma malha em uma única na única página.

    Diviewta-se.

    Saudações,

    Haps.

    Aqui está um código de exemplo que descreve o uso do spinner e a ação executada quando o item giratório é selecionado

     import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//airray of strings used to populate the spinner @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); //setting airray adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an airray of airbitrairy objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } } import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//airray of strings used to populate the spinner @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); //setting airray adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an airray of airbitrairy objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } } import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//airray of strings used to populate the spinner @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); //setting airray adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an airray of airbitrairy objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } }); import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//airray of strings used to populate the spinner @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); //setting airray adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an airray of airbitrairy objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } } import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//airray of strings used to populate the spinner @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Oviewride public void onItemSelected(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Oviewride public void onNothingSelected(AdapterView<?> airg0) { // TODO Auto-generated method stub } }); //setting airray adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an airray of airbitrairy objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } 

    Paira adicionair uma list de valores ao spinner, você precisa especificair um SpinnerAdapter em sua atividade, que se estende da class Adapter. Um adaptador giratório permite definir duas visualizações diferentes: uma que mostra os dados no próprio spinner e um que mostra os dados na list suspensa quando o spinner é pressionado. Você também pode download e consultair o projeto de exemplo spinner_demo completo paira uma melhor compreensão.

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