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 Build an Interactive Story App (Retired) Intents and Multiple Activities Getting Data from an Intent

Eleni Minadaki
Eleni Minadaki
3,687 Points

How to replace the mName in the app?

Hi, and happy new year to everyone! In the interactive story app, in the StoryActivity I don't need the mName, I need just the text. I should just delete and replace all the mName in the code with mText or should I do something else? Thanks a lot.

4 Answers

Right, OK. I've not done this course so I'm flying a bit blind here!

The mName is passed from MainActivity to StoryActivity via an intent. Let's see if the app will function if we don't pass the name via that intent. First, try commenting out this line in MainActivity:

intent.putExtra("TextView",TextView);

That stops the name being sent; so we need to stop it being expected over there by commenting out this line in StoryActivity:

mName = intent.getStringExtra(getString(R.string.key_name));

The only other place I can see it being used is in a string formatter. I think commenting out that line would be fine:

pageText  = String.format(pageText,mName);

I don't know what that does as I don't have the context of the course. Let's see what error it throws and we can deal with that from there.

I hope that helps, if not shout back!

Steve.

Hi Eleni,

Happy new year to you too!!

I'm not sure what it is you're trying to do here; have you misnamed a variable? You can rename variables by using the Refactor ... Rename function, but I'd prefer to be clear on what it is you want to do before I send you in the wrong direction!!

Steve.

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hi Steve, thanks for your reply. To be more clear what I meant: At the lessons give the ability the user put his name first and then begin the story. I would like to delete this ability, and when the app loads starts a story. But because I am newbie is confused with all this code and if something delete app stops. My code is below. Hope my explanation was clear. If needs to post another class please tell me.

Story.java
public class StoryActivity extends AppCompatActivity {

    public static final String TAG = StoryActivity.class.getSimpleName();

    private Story mStory = new Story();
    private ImageView mImageView;
    private TextView mTextView;
    private Button mChoice1;
    private Button mChoice2;
    private String mName;
    private Page mCurrentPage;
    private Page mPeviousPage;

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

        Intent intent = getIntent();
        mName = intent.getStringExtra(getString(R.string.key_name));

        if(mName == null){
            mName = "Friend";
        }

        Log.d(TAG, mName);

        mImageView = (ImageView)findViewById(R.id.storyImageView);
        mTextView = (TextView)findViewById(R.id.storyTextView);
        mChoice1 = (Button)findViewById(R.id.choiceButton1);
        mChoice2 = (Button)findViewById(R.id.choiceButton2);

        loadPage(0);
    }
    private void loadPage(int choice){
     mCurrentPage = mStory.getPage(choice);

        Drawable drawable  = getResources().getDrawable(mCurrentPage.getImageId());
        mImageView.setImageDrawable(drawable);

        String pageText = mCurrentPage.getText();
        //add the name if placeholder included.
        pageText  = String.format(pageText,mName);
        mTextView.setText(pageText);

        if(mCurrentPage.isFinal()){
            mChoice1.setVisibility(View.INVISIBLE);
            mChoice2.setText("Start From The Begining");
            mChoice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   finish();
                }
            });

        }else{

        mChoice1.setText(mCurrentPage.getChoice1().getText());
        mChoice2.setText(mCurrentPage.getChoice2().getText());

       mChoice1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               mCurrentPage.getChoice1().getNextPage();
               int nextPage = mCurrentPage.getChoice1().getNextPage();
               loadPage(nextPage);
           }
       });

        mChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentPage.getChoice2().getPreviousPage();
                int PreviousPage = mCurrentPage.getChoice2().getPreviousPage();
                loadPage(PreviousPage);
            }
        });
Main.java
public class MainActivity extends AppCompatActivity {

    private TextView meditCenterText;
    private Button mStartButton;

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

        meditCenterText = (TextView) findViewById(R.id.editCenterText);
        mStartButton = (Button) findViewById(R.id.startButton);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String TextView = meditCenterText.getText().toString();
               startStory(TextView);
            }
        });
    }
    private void startStory(String TextView){
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra("TextView",TextView);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        meditCenterText.setText("");
    }
Eleni Minadaki
Eleni Minadaki
3,687 Points

It works! Thanks a lot Steve.

No problem. Glad to help!