如何将哈希图中的值存储在String数组中

时间:2019-05-14 10:34:01

标签: java android api retrofit

我有一个从模型类中获取“键”字符串的循环,我想将这些值转换为String数组,但是我不确定该怎么做。基本上,我的应用程序是从api获取当前货币兑换的,而我是使用hashmap从中获取货币兑换的,我只想将这些值存储为数组。

Java类

public void retrofit() {
    Call<Model> call = api.getRatesByGivenCurrency(BASE_URL);
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(exchange_currency.this, response.code(), Toast.LENGTH_LONG).show();
            }
            list = Collections.singletonList(response.body());

            for (Model model : list) {
                Iterator<Map.Entry<String, Double>> entries = model.getRates().entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = (Map.Entry) entries.next();
                    keyString = (String) entry.getKey();
                    valueDouble = (Double) entry.getValue();
                }
            }
        }
        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Toast.makeText(exchange_currency.this, t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
 }
}

模型类

public class Model {

@SerializedName("base")
private String base;
@SerializedName("rates")
private Map<String, Double> rates;

public void setRates(Map<String, Double> rates) {
    this.rates = rates;
}

public void setBase(String base) {
    this.base = base;
}

public String getBase() {
    return base;
}

public Map<String, Double> getRates() {
    return rates;
}


class Rates {

    @SerializedName("BGN")
    private Double bGN;

    @SerializedName("NZD")
    private Double nZD;

    @SerializedName("ILS")
    private Double iLS;

    @SerializedName("RUB")
    private Double rUB;

    @SerializedName("CAD")
    private Double cAD;

    @SerializedName("USD")
    private Double uSD;

    @SerializedName("PHP")
    private Double pHP;

    @SerializedName("CHF")
    private Double cHF;

    @SerializedName("ZAR")
    private Double zAR;

    @SerializedName("AUD")
    private Double aUD;

    @SerializedName("JPY")
    private Double jPY;

    @SerializedName("TRY")
    private Double tRY;

    @SerializedName("HKD")
    private Double hKD;

    @SerializedName("MYR")
    private Double mYR;

    @SerializedName("THB")
    private Double tHB;

    @SerializedName("HRK")
    private Double hRK;

    @SerializedName("NOK")
    private Double nOK;

    @SerializedName("IDR")
    private Double iDR;

    @SerializedName("DKK")
    private Double dKK;

    @SerializedName("CZK")
    private Double cZK;

    @SerializedName("HUF")
    private Double hUF;

    @SerializedName("GBP")
    private Double gBP;

    @SerializedName("MXN")
    private Double mXN;

    @SerializedName("KRW")
    private Double kRW;

    @SerializedName("ISK")
    private Double iSK;

    @SerializedName("SGD")
    private Double sGD;

    @SerializedName("BRL")
    private Double bRL;

    @SerializedName("PLN")
    private Double pLN;

    @SerializedName("INR")
    private Double iNR;

    @SerializedName("RON")
    private Double rON;

    @SerializedName("CNY")
    private Double cNY;

    @SerializedName("SEK")
    private Double sEK;

    public Double getBGN() {
        return bGN;
    }

    public void setBGN(Double value) {
        this.bGN = value;
    }

    public Double getNZD() {
        return nZD;
    }

等...

Api界面

public interface Api {



@GET("latest")
Call<Model> getRatesByGivenCurrency(@Query("base") String base);

这是api https://exchangeratesapi.io/

3 个答案:

答案 0 :(得分:1)

如果我正确地理解了您,您想通过地图键创建 $('#id').on('click',function(){ // let a common class(disable-btn) for each button which should be disabled for on second $('.disable-btn').prop('disabled',true); setTimeout(function(){ // enable click after 1 second $('.disable-btn').prop('disabled',false); },1000); // 1 second delay });

您可以直接选择

String[]

答案 1 :(得分:0)

使用for循环获取Map的大小,然后将其添加到ArrayList

例如:

ArrayList<String> newArrayList = new ArrayList();
for(int i = 0 ; i< map.size; i++) {
newArrayList.add(map.get(i).get("keyname");
}

答案 2 :(得分:0)

您的问题不清楚!但是,例如,如果要将字符串转换为字符串数组,则可以使用如下代码:

String strArray[] = key.split(" ");

我认为您也可以使用list,并且每次迭代都将值添加到String列表中

List<String> keyList= new ArrayList<>();
keyList.add(key)