vincentvii6 vincentvii6 - 2 months ago 18
Android Question

setText use resource for placeholder

Following the instruction in Android Boot Camp to make an app for buying concert tickets.(very basic) My code is exacatly the same as the book.
At the end of the code the user inputs how many tickets they want and it should output the cost of the tickets for the group they picked. The last line is where I'm running into problems. I don't understand why it's not working and I haven't found anything that explains why this is wrong and how to fix it.

I've looked at the linked answer to see if it helped but it does not.

The following code is from MainActivity.java

public class MainActivity extends AppCompatActivity {
double costPerTicket = 79.99;
int numberOfTickets;
double totalCost;
String groupChoice;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tickets = (EditText)findViewById(R.id.txtTickets);
final Spinner group = (Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.btnCost);
cost.setOnClickListener(new View.OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));

@TargetApi(Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
numberOfTickets = Integer.parseInt(tickets.getText( ).toString( ));
totalCost = costPerTicket * numberOfTickets;
DecimalFormat currency = new DecimalFormat("$###,###.##");
groupChoice = group.getSelectedItem( ).toString( );
result.setText("Cost for " + groupChoice + "is " + currency.format(totalCost));

}
}
);

}


}

Answer

Here is the answer to fix the "Do not concatenate text" warning. (Please note that I didn't include your string.xml strings as I don't know the names of them.)

public class MainActivity extends AppCompatActivity {
    double costPerTicket = 79.99;
    int numberOfTickets;
    double totalCost;
    String groupChoice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText tickets = (EditText)findViewById(R.id.txtTickets);
        final Spinner group = (Spinner)findViewById(R.id.txtGroup);
        Button cost = (Button)findViewById(R.id.btnCost);
        cost.setOnClickListener(new View.OnClickListener() {
                                    final TextView result = ((TextView)findViewById(R.id.txtResult));

                                    @TargetApi(Build.VERSION_CODES.N)
                                    @Override
                                    public void onClick(View v) {
                                        numberOfTickets = Integer.parseInt(tickets.getText( ).toString( ));
                                        totalCost = costPerTicket * numberOfTickets;
                                        DecimalFormat currency = new DecimalFormat("$###,###.##");
                                        groupChoice = group.getSelectedItem( ).toString( );
                                        String costForString = "Cost for " + groupChoice + "is " + currency.format(totalCost);
                                        result.setText(costForString);

                                    }
                                }
        );

    }
}