我做完。我经常尝试用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”文档中了解它。
最佳答案
您需要使用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);
查看详细文档for the spinner和ArrayAdapter。
关于java - 我如何通过ArrayAdapter用ArrayList填充Spinner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41347923/