我如何通过ArrayAdapter用ArrayList填充Spinner?

时间:2016-12-27 15:32:30

标签: java android arraylist

我完成了。我经常尝试使用ArrayList-Content填充Spinner。

这是我的数据类:

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 [] RATE中的数据填充到我可以在此处看到的Spinner:

   <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” - 文档。

1 个答案:

答案 0 :(得分:0)

您需要使用ArrayAdapter(在您的Activity中):

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
</ul>

查看详细文档for the spinnerArrayAdapter