-3

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

        }
    }
    );

}

}

  • Possible duplicate of [Android TextView : "Do not concatenate text displayed with setText"](http://stackoverflow.com/questions/33164886/android-textview-do-not-concatenate-text-displayed-with-settext) – Mike M. Sep 19 '16 at 01:59
  • What exactly is the problem? Do you get some errors? – Anton Tananaev Sep 19 '16 at 02:34
  • I don't get errors at all. When I run the program I can type how many tickets I want and select the group I want, but when I click the button to show the cost of tickets the app closes with "Unfortunately, Concert Tickets has stopped." – vincentvii6 Sep 19 '16 at 02:37
  • @vincentvii6 Have you set the onClick method to the button in the XML file? – RamithDR Sep 19 '16 at 02:42
  • @RamithDR I believe so. The XML file that needs to be in is content_main.xml correct? – vincentvii6 Sep 19 '16 at 02:46
  • @vincentvii6 According to your latest edit, you've set an onClickListener to the button, you might want to remove any onClick methods defined to the button in your XML file. – RamithDR Sep 19 '16 at 02:54
  • @RamithDR I checked my XML file and I don't have an onClick method set on the button. It was done in the MainActivity.java – vincentvii6 Sep 19 '16 at 02:57
  • It wants you to put your string in strings.xml instead of directly inside of setText in Java or text in XML. – TheAnonymous010 Sep 19 '16 at 04:38
  • @TheAnonymous010 Do I need to make two different strings for "Cost for " and "is " then reference those in the code? What is the syntax to reference the strings? – vincentvii6 Sep 19 '16 at 04:52
  • As far as I know, you would need two strings. Once you create them in strings.xml, you can reference them by typing `@string/string_name` in replacement of "Costs for", etc. – TheAnonymous010 Sep 20 '16 at 14:29
  • @TheAnonymous010 When I try using @string/string_name I get, cannot resolve 'string'. – vincentvii6 Sep 22 '16 at 00:26
  • Sorry, the @string notation is used in XML. Replace what I told you with `R.string.your_string_name`, with your_string_name being the name of the string you created in strings.xml. – TheAnonymous010 Sep 22 '16 at 01:04
  • @TheAnonymous010 I changed it to R.string.cost_For but I still get the Do not use concatenate text with setText error. – vincentvii6 Sep 22 '16 at 01:26
  • Oh... See my answer. I will update your code to remove that error. Basically, it doesn't want you to append text inside of the setText method. You don't have to follow these instructions as they are just a warning, but I will create an answer to remove this. – TheAnonymous010 Sep 22 '16 at 02:48

1 Answers1

0

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

                                    }
                                }
        );

    }
}
TheAnonymous010
  • 725
  • 7
  • 19