A
A
andrej33372021-08-16 10:39:40
Java
andrej3337, 2021-08-16 10:39:40

How to convert currencies with calculation for any field?

The code is taken as a basis and partially reworked by me. While I'm reading a textbook on Java, I imagine what should happen and try to learn something in practice. It was possible to get exchange rates by api. There are 4 (actually 5) but important 4 TextViews and 4 currency label search spinners. Date button to select exchange rates by date.

The draft version works fine, except for one thing - the calculation is made by entering the amount of the base currency in a specific field, but I would like to enter the amount in any field with the calculation for other fields. Here's a question for connoisseurs how, within the framework of the existing code, this could be implemented

public void currencyInputs() {
    et1 = (EditText) findViewById(R.id.input1); //это
    et2 = (EditText) findViewById(R.id.input2); //те
    et3 = (EditText) findViewById(R.id.input3); //4
    et4 = (EditText) findViewById(R.id.input4); //поля
    et5 = (EditText) findViewById(R.id.basicValue); //тут еще расчет количества базовых величин (чисто белорусское изобретение)
    etArray.add(et1); //тут ссылки передаем в массив
    etArray.add(et2);
    etArray.add(et3);
    etArray.add(et4);
}


And here is the calculation

private void exchange() {
    EditText baseEt = (EditText) etArray.get(0); // берется первое поле из массива за базовое
    if (baseEt == null || TextUtils.isEmpty(baseEt.toString())) {
        Log.e("EditText Error", "EditText Missing");
        return;
    }

    double baseValue = (Double) Double.valueOf(baseEt.getText().toString());
    //получаем из него содержимое и передаем в цикл и формулу
    for (int i = 1; i < 4; i++) {
        double convertedValue = baseValue / rate[i]; //rate[i] это массив переменных из курсов валют, которые выбраны в спиннерах 

        etArray.get(i).setText(String.valueOf(convertedValue));

    }

    double baseVal = baseValue / 29.0;
    et5.setText(String.valueOf(baseVal));
}


Here is the result for today
611a15a3b9a21656398402.jpeg

. There is an idea to somehow take for the base field not with a zero index - etArray.get(0), but the one on which the cursor or focus is located, but it is not entirely clear how to solve this problem, there is not enough experience and knowledge.

Maybe someone has ideas?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Orkhan, 2021-08-16
Hasanly @azerphoenix

Good afternoon!
The implementation logic is approximately the following:
you need some event listener that tracks field changes. If one of the fields has been changed, then we take the value of this field and get the type (currency) of this field. And then we send a request via the API and get the corresponding quotes.

J
Jacen11, 2021-08-17
@Jacen11

andrej3337 ,

can a separate listener for each field do

yes, what's the problem?
it looks like you got a hammer in your hands and you started hammering everything with it. It is better not to use an array because it will be difficult to scale. You can then add fields in the middle, end, beginning
A listener hung on them - all of the 4 fields were written to the log from it

is there an example? you can hang on each, you can and all at once. Depends on the number of fields
well and on trifles
for (int i = 1; i < 4; i++) {

better for (int i = 1; i < etArray.size; i++) and even better forEach loop

A
andrej3337, 2021-08-17
@andrej3337

Here is a listener hung for each field

private void getOnFocus0() {
                etArray.get(0).setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        public void onFocusChange(View v, boolean hasFocus) {
                                if (hasFocus) {
                                        EditText baseEt = (EditText) etArray.get(0);
                                        Log.e("id0", ""+baseEt);
                                }
                        }
                });
        }

When you click on the field, it shows the one you clicked on, the output is
E/id0: androidx.appcompat.widget.AppCompatEditText{51f7120 VFED..CL. .FP.ID 315.1-889.167 #7f0900e0 app:id/input1}

How to transfer this information to another action, let's say the same exchange () to say there that if this app: id / input1, i.e. etArray.get(0) worked, need to exclude it from ArrayList - etArray.remove(0)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question