如何在不更改相同值的情况下填充具有不同名称的字符串数组项?

时间:2015-07-15 06:46:36

标签: android arrays json

我是Android开发的新手。 在我的项目中我有一个微调器,字符串数组和一个JSON适配器,我想要的只是我的字符串数组必须显示不同的数组项名称但仍具有相同的值

注意 JSON数据在线。

    public class CurrencyFragment extends Fragment
{
    Spinner text_meter, spinner2;
    Button button_meter;
    EditText mEdit;
    TextView result1, result2, result3, result4, result5,result6,result7, result8, result9, result10, result11, result12;


    MyRestClient myRestClient;

    public CurrencyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_currency, container, false);
        text_meter = (Spinner) view.findViewById(R.id.spinner2);
        button_meter = (Button)view.findViewById(R.id.button);
        mEdit = (EditText)view.findViewById(R.id.Text_value);
        final SharedPreferences pref = getActivity().getSharedPreferences("asd", Context.MODE_PRIVATE);



        spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.Currency, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);

        myRestClient.getClient().getCurrencies(new Callback<JsonObject>() {
            @Override
            public void success(JsonObject jsonObject, Response response) {
                JsonPrimitive timeStampDate = jsonObject.getAsJsonObject().getAsJsonPrimitive("timestamp");
                final JsonObject quotes = jsonObject.getAsJsonObject("quotes");
                //final JsonObject date = jsonObject.date("timestamp");
                final JsonParser jp = new JsonParser();
                final JsonObject conversionObject = (JsonObject) jp.parse(String.valueOf(quotes));

                //saving data
                SharedPreferences.Editor prefsEditor = pref.edit();
                Gson gson = new Gson();
                String json = gson.toJson(quotes);
                prefsEditor.putString("quotes", json);
                prefsEditor.commit();


                //Getting data
                Gson gson1 = new Gson();
                String json1 = pref.getString("quotes", null);
                JsonObject inMemoryRates = (JsonObject) jp.parse(String.valueOf(json1));

                inMemoryRates.get("USD2EUR");

               // quotes obj = gson.fromJson(json, quotes.class);
                Log.d("LOL",json1);

                final Float usd2inr1 = Float.valueOf(inMemoryRates.get("USDEUR").toString());
                final Float usd2inr = Float.valueOf(quotes.get("USDINR").toString());
                //Float datetime = Float.valueOf(timeStampDate.get(("timeStampDate")).toString());
                final Float usd2eur = Float.valueOf(quotes.get("USDEUR").toString());
                Toast.makeText(getActivity(), "USD to EURO rate is " + usd2inr1, Toast.LENGTH_SHORT).show();
                button_meter.setOnClickListener(
                        new View.OnClickListener() {

                            public void onClick(View view) {

                                Float divisor = Float.valueOf(conversionObject.get(spinner2.getSelectedItem().toString()).getAsString());

                                Float val = Float.valueOf(mEdit.getText().toString()) / divisor;

                                if (spinner2.getSelectedItem().toString() == "USDINR") {
                                    Toast.makeText(getActivity(), "USD to EURO rate is " + usd2eur, Toast.LENGTH_SHORT).show();
                                }

                                result1.setText(String.valueOf(val * Float.valueOf(quotes.get("USDEUR").toString())));
                                result2.setText(String.valueOf(val * Float.valueOf(quotes.get("USDGBP").toString())));
                                result3.setText(String.valueOf(val * Float.valueOf(quotes.get("USDCAD").toString())));
                                result4.setText(String.valueOf(val * Float.valueOf(quotes.get("USDPLN").toString())));
                                result5.setText(String.valueOf(val * Float.valueOf(quotes.get("USDUSD").toString())));
                                result6.setText(String.valueOf(val * Float.valueOf(quotes.get("USDINR").toString())));
                                result7.setText(String.valueOf(val * Float.valueOf(quotes.get("USDMXN").toString())));
                                result8.setText(String.valueOf(val * Float.valueOf(quotes.get("USDAUD").toString())));
                                result9.setText(String.valueOf(val * Float.valueOf(quotes.get("USDBGN").toString())));
                                result10.setText(String.valueOf(val * Float.valueOf(quotes.get("USDCZK").toString())));
                                result11.setText(String.valueOf(val * Float.valueOf(quotes.get("USDCNY").toString())));
                                result12.setText(String.valueOf(val * Float.valueOf(quotes.get("USDDKK").toString())));

                            }
                        });

            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "Could not fetch Live currencies ", Toast.LENGTH_SHORT).show();
                final JsonParser jp = new JsonParser();
                Gson gson1 = new Gson();
                final String json1 = pref.getString("quotes", null);
                final JsonObject inMemoryRates = (JsonObject) jp.parse(String.valueOf(json1));

                final Float usd2inr1 = Float.valueOf(inMemoryRates.get("USDINR").toString());
                //Toast.makeText(getActivity(), "USD to EURO rate is " + usd2inr1, Toast.LENGTH_SHORT).show();

                button_meter.setOnClickListener(
                        new View.OnClickListener() {

                            public void onClick(View view) {

                                Float divisor = Float.valueOf(inMemoryRates.get(spinner2.getSelectedItem().toString()).getAsString());

                                Float val = Float.valueOf(mEdit.getText().toString()) / divisor;


                                result1.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDINR").toString())));
                                result2.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDGBP").toString())));
                                result3.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDCAD").toString())));
                                result4.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDPLN").toString())));
                                result5.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDUSD").toString())));
                                result6.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDINR").toString())));
                                result7.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDMXN").toString())));
                                result8.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDAUD").toString())));
                                result9.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDBGN").toString())));
                                result10.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDCZK").toString())));
                                result11.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDCNY").toString())));
                                result12.setText(String.valueOf(val * Float.valueOf(inMemoryRates.get("USDDKK").toString())));

                            }
                        });



            }
        });

        return view;
    }


}

我的字符串数组项

<string-array name="Currency">
    <item>USDEUR</item>
    <item>USDGBP</item>
    <item>USDCAD</item>
    <item>USDPLN</item>
    <item>USDUSD</item>
    <item>USDINR</item>
    <item>USDMXN</item>
    <item>USDAUD</item>
    <item>USDBGN</item>
    <item>USDCZK</item>
    <item>USDCNY</item>
    <item>USDDKK</item>
</string-array>

这是REST客户端类 公共类MyRestClient {

   private static RestInterface restInterface;

    public static RestInterface getClient(){
        if( restInterface == null ) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint("http://apilayer.net/api")
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .build();
            restInterface = restAdapter.create(RestInterface.class);
        }
        return restInterface;
    }

    public interface RestInterface {

        @GET("/live?access_key=7c1ae65f4e0d77ead7b169bb4bd0f90&currencies=EUR,GBP,CAD,PLN,USD,INR,MXN,JPN,AUD,BGN,CZK,CNY,DKK&source=USD&format=1")
        void getCurrencies(Callback<JsonObject> callback);

    }
}

我希望我的微调器显示出类似这样的东西 美元, 印度卢比
一个不同的名称,而不是USDEUR,USDCAD,USDGBP ......等等。

1 个答案:

答案 0 :(得分:1)

您可以通过在资源中定义另一个数组来解决此问题,该数组具有与相同索引的Currency数组相对应的“常识”名称。喜欢这个

<string-array name="CurrencyMappings">
<item>US Dollar to Euro</item>
...
<item>US Dollar to DKK</item>
</string-array>

然后更改处理UI的代码以在微调器中显示此数组,基本上将Currency替换为CurrencyMappings

现在从用户选择中获取转换代码,您需要更改

Float divisor = Float.valueOf(conversionObject.get(spinner2.getSelectedItem().toString()).getAsString());

因此,不是从spinner2中获取选定项,而是从spinner 2中获取所选项的索引,该索引现在具有常识值,并使用此索引从Currency数组中获取转换代码。然后该值成为除数。

此外,如果您使用的是XMl布局,则可以添加

android:entries="@array/CurrencyMappings"

到你的微调器布局,所以你不必在代码中填充它。