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 Testing in Android Unit Testing Viewing the Presenter

Trainer Workout
Trainer Workout
22,341 Points

What is missing in the presenter ?

Here's a simple counter app consisting of only a TextView and a Button. When the Button is pressed the counter (TextView) increases by one. Refactor this to now use a View and Presenter.

Bummer! Don't forget to create a new constructor in the presenter!

What is missing in the presenter ? I already have a constructor in there.

MainActivity.java
public class MainActivity extends AppCompatActivity implements MainActivityView {
    TextView counter;
    Button button;

    MainActivityPresenter presenter;

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

        presenter = new MainActivityPresenter(this);

        counter = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int count = Integer.parseInt(counter.getText().toString());
                presenter.buttonClicked(Integer.toString(count + 1));
            }
        });
    }

    @Override
    public void changeTextViewText(String text) {
        counter.setText(text);
    } //changeTextViewText()
}
MainActivityView.java
public interface MainActivityView {
    void changeTextViewText(String text);
}
MainActivityPresenter.java
public class MainActivityPresenter {
    MainActivityView view;

    public MainActivityPresenter(MainActivity view) {
        this.view = view;
    } // MainActivityPresenter()

    public void buttonClicked(String text) {
        view.changeTextViewText(text);
    } // editTextUpdated()
}
Trainer Workout
Trainer Workout
22,341 Points

I changed my constructor in MainActivityPresenter to this but still no luck

public MainActivityPresenter(MainActivityView view) {
        this.view = view;
    } // MainActivityPresenter()

3 Answers

Hello, I tried to above code blocks, but I have taken same error message "Don't forget to create a new constructor in the presenter!"

Guilherme Cunha
Guilherme Cunha
1,648 Points

I'm experiencing the some problem, how did you solve it? Why u don't put the sum function in presenter?

Trainer Workout
Trainer Workout
22,341 Points

I moved the calculation to the presenter, but it still does not work.

Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Guilherme! I'm not entirely sure what's going on here, could you post your code to help me figure it out?

Trainer Workout
Trainer Workout
22,341 Points

I tested the code above with the the second constructor. It works now :)