2016-12-27 81 views
1

我完成了。我經常嘗試用一個ArrayList-Content填充一個Spinner。如何通過ArrayAdapter使用ArrayList填充微調器?

這是我的數據類:

package com.example.elmomac.currencyconverter; 

import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Map; 

public class ExchangeRateDatabase { 
    // Exchange rates to EURO - price for 1 Euro 
    private final static ExchangeRate[] RATES = { 
      new ExchangeRate("EUR", 1.0), 
      new ExchangeRate("USD", 1.0845), 
      new ExchangeRate("JPY", 130.02), 
      new ExchangeRate("BGN", 1.9558), 
      new ExchangeRate("CZK", 27.473), 
      new ExchangeRate("DKK", 7.4690), 
      new ExchangeRate("GBP", 0.73280), 
      new ExchangeRate("HUF", 299.83), 
      new ExchangeRate("PLN", 4.0938), 
      new ExchangeRate("RON", 4.4050), 
      new ExchangeRate("SEK", 9.3207), 
      new ExchangeRate("CHF", 1.0439), 
      new ExchangeRate("NOK", 8.6545), 
      new ExchangeRate("HRK", 7.6448), 
      new ExchangeRate("RUB", 62.5595), 
      new ExchangeRate("TRY", 2.8265), 
      new ExchangeRate("AUD", 1.4158), 
      new ExchangeRate("BRL", 3.5616), 
      new ExchangeRate("CAD", 1.3709), 
      new ExchangeRate("CNY", 6.7324), 
      new ExchangeRate("HKD", 8.4100), 
      new ExchangeRate("IDR", 14172.71), 
      new ExchangeRate("ILS", 4.3019), 
      new ExchangeRate("INR", 67.9180), 
      new ExchangeRate("KRW", 1201.04), 
      new ExchangeRate("MXN", 16.5321), 
      new ExchangeRate("MYR", 4.0246), 
      new ExchangeRate("NZD", 1.4417), 
      new ExchangeRate("PHP", 48.527), 
      new ExchangeRate("SGD", 1.4898), 
      new ExchangeRate("THB", 35.328), 
      new ExchangeRate("ZAR", 13.1446) 
    }; 

    private final static Map<String, Double> CURRENCIES_MAP = new HashMap<>(); 

    private final static String[] CURRENCIES_LIST; 

    static { 
     for (ExchangeRate r : RATES) { 
      CURRENCIES_MAP.put(r.getCurrencyName(), r.getRateForOneEuro()); 
     } 
     CURRENCIES_LIST = new String[CURRENCIES_MAP.size()]; 

     CURRENCIES_MAP.keySet().toArray(CURRENCIES_LIST); 
     Arrays.sort(CURRENCIES_LIST); 

    } 

    /** 
    * Returns list of currency names 
    */ 

    public String[] getCurrencies() { 
     return CURRENCIES_LIST; 
    } 

    /** 
    * Gets exchange rate for currency (equivalent for one Euro) 
    */ 

    public double getExchangeRate(String currency) { 
     return CURRENCIES_MAP.get(currency); 
    } 

    /** 
    * Converts a value from a currency to another one 
    * @return converted value 
    */ 
    public double convert(double value, String currencyFrom, String currencyTo) { 
     return value/CURRENCIES_MAP.get(currencyFrom) * CURRENCIES_MAP.get(currencyTo); 
    } 
} 

如何從EXCHANGERATE []率的數據填充到我的微調,你可以在這裏看到:

<Spinner 
     android:id="@+id/cur_from" 
     android:layout_width="100px" 
     android:layout_height="100px" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textAlignment="viewStart"> 
    </Spinner> 

我沒有膠水如何開始。也許有人可以給我一些提示,因爲我根本無法從「AndroidDeveloper」文檔中理解它。

回答

0

你需要使用一個ArrayAdapter(您的活動):

ExchangeRateDatabase exchangeRateDatabase = new ExchangeRateDatabase(); 
Spinner mySpinner = (Spinner) findViewById(R.id.cur_from); 
ArrayAdapter<String> myAdapter = new CustomizedSpinnerAdapter(
     AddSlaveActivity.this, android.R.layout.simple_spinner_item, 
     exchangeRateDatabase.getCurrencies()); 
mySpinner.setAdapter(myAdapter); 

退房的詳細文檔for the spinnerArrayAdapter