0

I'm new to android development. in my project I have a spinner, string array and a JSON adapter,all I want is my string array must show different array item names but still have same value.

Note JSON data is online.

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


}

My string array item

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

This is REST client class public class 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);

    }
}

I want my spinner to show some thing like this US Dollar, Indian Rupee
an so with different names instead of USDEUR,USDCAD,USDGBP...etc

1
  • have another parallel array with those values Commented Jul 15, 2015 at 6:50

1 Answer 1

1

You could work around it by defining an another array in your resources, that has the corresponding "common sense" names to your Currency array with the same indexes. Like this

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

Then change your code that deals with UI to display this array in the spinner, basically replace Currency with CurrencyMappings.

Now to get the conversion codes from the user selection you need to change

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

so that instead of getting selected item from spinner2, you get the index of the selected item from spinner 2 that now has the common sense values, and use this index to get the conversion code from Currency array. This value then becomes the divisor.

Additionally, if you are using XMl layouts, you can add

android:entries="@array/CurrencyMappings"

to your spinner layout, so you dont have to populate it in code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.