2017-05-26 65 views
0

我正在使用動態和靜態微調。 這是我的代碼,我如何實現HashMap來顯示用戶選擇的內容(值)?在spinners中使用HashMaps android studio

public class DatosAutoActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

    private Spinner sp_marca, sp_modelo, sp_colores; 

    private TextView tvMarca, tvModelo, tvColor, tvPlacas; 
    private Button btn_enviarDatos; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_datos_auto); 


     sp_marca = (Spinner)findViewById(R.id.sp_marca); 
     sp_modelo = (Spinner)findViewById(R.id.sp_modelo); 
     sp_colores = (Spinner)findViewById(R.id.sp_colores); 


     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, 
       R.array.array_marcas, 
       android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     sp_marca.setAdapter(adapter); 
     sp_marca.setOnItemSelectedListener(this); 

     ArrayAdapter<CharSequence> adapterColor = ArrayAdapter.createFromResource(
       this, 
       R.array.array_colores, 
       android.R.layout.simple_spinner_item); 
     adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     sp_colores.setAdapter(adapterColor); 
     sp_colores.setOnItemSelectedListener(this); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
           long id) { 
     int[] marcas = {R.array.array_chevrolet, R.array.array_ford, R.array.array_honda, 
       R.array.array_nissan,R.array.array_renault, R.array.array_toyota, 
       R.array.array_volkswagen}; 

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, 
       marcas[position], 
       android.R.layout.simple_spinner_item); 

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     sp_modelo.setAdapter(adapter); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     btn_enviarDatos = (Button)findViewById(R.id.btn_enviarDatos); 

     btn_enviarDatos.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Intent siguiente = new Intent(DatosAutoActivity.this, VerificacionDatosActivity.class); 
        startActivity(siguiente); 
       } 
       else { 
        Toast.makeText(getApplicationContext(), "Debes llenar todos los campos", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 
+0

待辦事項你想將'HashMap'數據傳遞給適配器? –

回答

0

使用的Hashmap希望這會幫助你。

價值微調

String[] spinnerArray = new String[array.size()]; 
HashMap<Integer,String> spinnerMap = new HashMap<Integer, String>(); 
for (int i = 0; i < array.size(); i++) 
{ 
    spinnerMap.put(i,array.get(i)); 
    spinnerArray[i] = array.get(i); 
} 

設定值從微調微調器

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, spinnerArray); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 

獲得價值

String name = spinner.getSelectedItem().toString(); 
String id = spinnerMap.get(spinner.getSelectedItemPosition()); 
+0

我試圖做這樣的事情: HashMap datos = new HashMap <>(); (「雪佛蘭」,「雪佛蘭」); datos.put(「雪佛蘭」,「雪佛蘭」); –

+0

例如,我有這些值:「<字符串數組名= 「array_marcas」> 雪佛蘭 福特 本田 日產 雷諾 豐田 大衆 < /串陣列> <字符串數組名= 「array_chevrolet」> 雪佛蘭 火花 馬里布