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 trialsean tatenda jack
6,127 PointsHere's a simple counter app consisting of only a TextView and a Button. When the Button is pressed the counter (TextVie
i cant figure out where i an getting this code wrong
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.
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);
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());
counter.setText(Integer.toString(count + 1));
}
});
}
}
public interface MainActivityView {
void buttonPressed(TextView counter);
}
public class MainActivityPresenter {
private MainActivityView view;
public MainActivityPresenter(MainActivityView view) {
this.view = view;
}
public void onButtonPressed(TextView counter) {
view.buttonPressed(counter);
}
}
1 Answer
Bryan Wiedeman
10,203 PointsI know this is extremely late, but the issue is that while you're implementing MainActivityView, you aren't overriding it's methods in MainActivity. Let me know if you need this described more and I'll try to get more into it.