简体   繁体   English

如果从微调框选择了项目,则来自类的Android Java调用方法

[英]Android Java call method from class if item is selected from spinner

I'm not sure how to title this question, but basically we are building a currency converter app and we have a JSON file that looks like this: 我不确定如何为这个问题加上标题,但是基本上我们正在构建一个货币转换器应用程序,并且我们有一个如下所示的JSON文件:

{
  "base": "SGD",
  "date": "2016-02-26",
  "rates": {
    "AUD": 0.99008,
    "BGN": 1.2677,
    "BRL": 2.8102,
    "CAD": 0.96636,
    "CHF": 0.70839,
    "CNY": 4.6639,
    "CZK": 17.542,
    "DKK": 4.8354,
    "GBP": 0.5104,
    "HKD": 5.5426,
    "HRK": 4.941,
    "HUF": 201.27,
    "IDR": 9537.9,
    "ILS": 2.7826,
    "INR": 49.002,
    "JPY": 80.646,
    "KRW": 881.73,
    "MXN": 12.914,
    "MYR": 3.0044,
    "NOK": 6.1735,
    "NZD": 1.0596,
    "PHP": 33.884,
    "PLN": 2.828,
    "RON": 2.8934,
    "RUB": 53.827,
    "SEK": 6.074,
    "THB": 25.432,
    "TRY": 2.0966,
    "USD": 0.71338,
    "ZAR": 11.183,
    "EUR": 0.64817
  }
}

And we have a "rates" class that basically contains everything inside the JSON file after the "rates". 我们有一个“费率”类,该类基本上包含“费率”之后JSON文件中的所有内容。

private double AUD; ... 

Aand we have a spinneradapter class where we are able to choose what currency we want to convert to, and we need to call the GET method when that item from the spinner. 再有,我们有一个spinneradapter类,我们可以在其中选择要转换为哪种货币,当需要来自微调器的项目时,我们需要调用GET方法。 My current solution is to use the traditional method of if-else or cases to retrieve the data. 我当前的解决方案是使用if-else或case的传统方法来检索数据。

if( ((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString() == "AUD" )
    {
            myRates.getAUD();
    }

But we'll need to do this consistently for 30 other variables. 但是我们需要对其他30个变量进行一致的处理。 Is there any better way to work around this? 有没有更好的方法来解决此问题?

Getting the rates from a parser: 从解析器获取费率:

protected void onPostExecute(JSONArray json) {

        if (json != null) {
            // looping through All records
            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);

                    rates.setAUD(c.getDouble("AUD"));
                    rates.setBRL(c.getDouble("BRL"));
                    rates.setBGN(c.getDouble("BGN"));
                    rates.setCAD(c.getDouble("CAD"));
                    rates.setCNY(c.getDouble("CNY"));

                    rates.setCHF(c.getDouble("CHF"));
                    rates.setCZK(c.getDouble("CZK"));
                    rates.setDKK(c.getDouble("DKK"));
                    rates.setEUR(c.getDouble("EUR"));
                    rates.setGBP(c.getDouble("GBP"));

                    rates.setHKD(c.getDouble("HKD"));
                    rates.setHRK(c.getDouble("HRK"));
                    rates.setHUF(c.getDouble("HUF"));
                    rates.setIDR(c.getDouble("IDR"));
                    rates.setILS(c.getDouble("ILS"));

                    rates.setINR(c.getDouble("INR"));
                    rates.setJPY(c.getDouble("JPY"));
                    rates.setKRW(c.getDouble("KRW"));
                    rates.setMXN(c.getDouble("MXN"));
                    rates.setMYR(c.getDouble("MYR"));

                    rates.setNOK(c.getDouble("NOK"));
                    rates.setNZD(c.getDouble("NZD"));
                    rates.setPHP(c.getDouble("PHP"));
                    rates.setPLN(c.getDouble("PLN"));
                    rates.setRON(c.getDouble("RON"));

                    rates.setRUB(c.getDouble("RUB"));
                    rates.setSEK(c.getDouble("SEK"));
                    rates.setTRY(c.getDouble("TRY"));
                    rates.setTHB(c.getDouble("THB"));
                    rates.setUSD(c.getDouble("USD"));
                    rates.setZAR(c.getDouble("ZAR"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }

    }
}

Possible options are 可能的选项是

  • Use switch case instead of if else : if you are using java 7 or greater then only switch case of string will work. 请使用switch case而不是if else :如果您使用的是Java 7或更高版本,则仅string的case可以使用。
  • Create a HashMap of String to Double : and add all rates in that HashMap 创建一个String的HashMap到Double :并在该HashMap添加所有rates
  • Implementing the cases as elements of an Enum may help somewhat - example 将案例作为枚举的元素来实现可能会有所帮助- 示例

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

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