Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

I am doing an android App, but i am couldn't make it work

I am trying to combine the new generated number to the previous generated number, so every click will add one digit to the numberList

======================================================================== public class playActivity extends AppCompatActivity {

private randomText mRandomText = new randomText();
private getNewNumber mGetNewNumber = new getNewNumber();


private TextView mGameText;
private TextView mNewNumberWord;
private TextView mNewNumber;
private TextView mLevel;
private TextView mAnswerWord;
private EditText mAnswer;
private Button mPlayButton;

private int gameLevel = 0;

// the next line will generate random number from 0-9 <<<<<<<<<<<<<<<<<<<<<<<<<
private String randNumber = mGetNewNumber.getGenNumber();

private String numberList = "";

// I am trying to combine the new generated number to the previous generated number, so every click will add one digit to the numberList <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        numberList += randNumber;  <<<<<<this line is not working!!


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);


    mGameText = (TextView) findViewById(R.id.funText);
    mNewNumberWord = (TextView) findViewById(R.id.newNumberWord);
    mNewNumber = (TextView) findViewById(R.id.newNumber);
    mLevel = (TextView) findViewById(R.id.showLevelText);
    mAnswerWord = (TextView) findViewById(R.id.answerWord);
    mAnswer = (EditText) findViewById(R.id.answer);
    mPlayButton = (Button) findViewById(R.id.playButton);


    mNewNumber.setText(randNumber);
    mAnswerWord.setText(numberList);


    mPlayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            gameProcess();

        }
    });
}


private void gameProcess() {

    String text = mRandomText.getRandomText();
    mGameText.setText(text);

    mNewNumber.setText(mGetNewNumber.getGenNumber());

    gameLevel++;
    mLevel.setText("Level: " + gameLevel);
}

}

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Yiu! Both 'randNumber' and 'numberList' are Strings, so you're not able to add them like that. Try converting those Strings to ints and then they should add just fine.

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

Thank you for help, i fingered i forgot to send out the string from the last activity.

Also, i am want wondering is anyway i can send out the string from the first activity to the third or fourth activity. For example, I input an edittext string from activity1. At the activity2 i will just showing some general info of the game. Then, i want to use the edittext string(from activity 1) at activity 3.

How can do it? I will really appreciate for your help

Ben Deitch
Ben Deitch
Treehouse Teacher

Just send it to activity2 and then to activity3. You don't have to use it in activity2 :)