T
T
ti12018-06-21 09:55:23
Java
ti1, 2018-06-21 09:55:23

Android/Java - how to output multiple variables per line?

I'm making a simple coffee ordering app based on one course from Udacity , just adding my own features, because they leave a lot of "holes".
In general, the user there clicked on the required number of cups of coffee, added cream and / or chocolate as desired, and pressed the "order" button.
Depending on what he clicked there, the strings.xml file stored lines like:

<string name="total_coffee_cost">You ordered %1$d cups of coffee. \nPrepare to pay $%2$d. \nThank you!</string>
    <string name="total_coffee_cost_zero">You have not ordered any cup of coffee. \nTry to choose again. \nThank you!</string>

All this was displayed in MainActivity.java via setText(), depending on the amount of selected coffee with toppings (there are nine if-else branches, I give two so as not to clog):
private TextView createOrderSummary(View view){
        CheckBox whippedCreamCheckBox = findViewById(R.id.whipped_cream_checkbox);
        hasWhippedCream = whippedCreamCheckBox.isChecked();

        CheckBox chocolateCheckBox = findViewById(R.id.chocolate_checkbox);
        hasChocolate = chocolateCheckBox.isChecked();
        
        TextView priceMessage = findViewById(R.id.price_text_view);
        if (numberOfCoffees == 0)
            priceMessage.setText(getString(R.string.total_coffee_cost_zero));
        ....
        else if ((numberOfCoffees > 1) && (!hasWhippedCream) && (!hasChocolate))
        priceMessage.setText(getString(R.string.total_coffee_cost, numberOfCoffees, numberOfCoffees * priceOfCup));
        ....
        return priceMessage;
}

Everything was fine, but then in the course they decided to add another variable - the username that he enters when ordering. And it turned out that I can’t just take and add a third variable to the line, for example, by doing this:
<string name="total_coffee_cost">Hi, %1$s! You ordered %2$d cups of coffee. \nPrepare to pay $%3$d. \nThank you!</string>

Googling, I found out that the setText () method cannot work with three or more parameters ...
How to be? The Udacity course doesn't bother with this - they output data directly, without storing strings in strings.xml, without using setText().
There must be a normal solution, because these parameters may not be three, like mine, but much more ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2018-06-21
@iLLuzor

What do the parameters of the setText() method have to do with it? You are passing arguments to getString() . It can have any number of formatArgs arguments. Read about varargs.
I'm guessing you're just a little confused. For convenience, declare a variable for the text:

String textString = getString(R.string.total_coffee_cost, numberOfCoffees, numberOfCoffees * priceOfCup, param3, param4);
priceMessage.setText(textString);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question