简体   繁体   English

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

[英]How i fill a Spinner with a ArrayList through a ArrayAdapter?

I am done. 我做完。 I tried it often to fill a Spinner with an ArrayList-Content. 我经常尝试使用ArrayList-Content填充Spinner。

This is the class with my data: 这是我的数据类:

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);
    }
}

How can i fill the data from ExchangeRate [] RATE into my Spinner you can see here: 如何将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>

I have no glue how to start. 我没有胶水如何开始。 Maybe someone can give me some hints, because i can't understand it at all just from the "AndroidDeveloper"-Documentation. 也许有人可以给我一些提示,因为我根本无法理解它只是来自“AndroidDeveloper” - 文档。

You need to use an ArrayAdapter (in your Activity): 您需要使用ArrayAdapter(在您的Activity中):

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); 

Check out the detailed documentation for the spinner and the ArrayAdapter . 查看微调器ArrayAdapter的详细文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM