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

Tom Finet
Tom Finet
7,027 Points

Refactoring code to use View and Presenter.

What is the problem with this code?

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

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

        MainActivityPresenter map = 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) {
                map.onButtonPressed(counter);
            }
        });
    }

  @Override
  public void buttonPressed(TextView counter) {
     int count = Integer.parseInt(counter.getText().toString());
     counter.setText(Integer.toString(count + 1));
  }

}
MainActivityView.java
public interface MainActivityView {
  void buttonPressed(TextView counter);
}
MainActivityPresenter.java
public class MainActivityPresenter {

  private MainActivityView view;

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

  public void onButtonPressed(TextView counter) {
    view.buttonPressed(counter);
  }

}

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

The error the code gives is "Don't forget to create a MainActivityPresenter field in your Activity." This means it wants you to use a member variable for MainActivityPresenter instead of a local one. Making this one change will pass the challenge.

public class MainActivity extends AppCompatActivity implements MainActivityView {
    TextView counter;
    Button button;
    MainActivityPresenter map;


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

        map = new MainActivityPresenter(this);
   // ...Rest of code is fine...
Seth Kroger
Seth Kroger
56,414 Points

One other thing is although it passes, you don't need to pass the TextView for counter between the presenter and view. MainActivity already has a field for counter. The View/Presenter model is supposed to help isolate your user interface logic from dependence on the Android OS (or any other).

Tom Finet
Tom Finet
7,027 Points

Ok thanks. I understand now. =)