Jumat, 11 November 2011

[DIY Tutorial] Implement MVP on Android ~ DroidUMM

This is a tutorial on applying MVP android from the previous material. I took this article from http://jamespeckham.com following article is very simple to be implemented so you need to do the implementation.

Now you make your android project. finish creating a project android, created three packages.


create a class with the name AskNameView.java used as a Main Activity or up to you if you have any other name that you created by the wizard



package com.jpeckham.examples;

import com.jpeckham.examples.presenters.AskNamePresenter;
import com.jpeckham.examples.views.IAskNameView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class AskNameView extends Activity implements IAskNameView {
    /** Called when the activity is first created. */
 AskNamePresenter _presenter;
 
 public AskNameView(){
  _presenter = new AskNamePresenter(this);
 }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 public void setDisplayedText(String text) {
  getShownLabel().setText(text);
 }
 
 public void submitClicked(View widget) {
  _presenter.EnterNameAndAge(getNameField().getText().toString(), 
    Integer.parseInt(getAgeField().getText().toString()));
 }
 
 private EditText getNameField() {
  return (EditText) findViewById(R.id.name);
 }
 private EditText getAgeField(){
  return (EditText) findViewById(R.id.age);
 }
 private TextView getShownLabel(){
  return (TextView) findViewById(R.id.shown);
 }
}
Next on the package com.jpeckham.examples.presenters, you create a new class with the name AskNamePresenter. This class is used as the presenters of the MVP

package com.jpeckham.examples.presenters;

import com.jpeckham.examples.views.IAskNameView;

public class AskNamePresenter {
 private IAskNameView _view;
 public AskNamePresenter(IAskNameView view){
  _view = view;
 }
 public void EnterNameAndAge(String name, int age) {
  _view.setDisplayedText(String.format("%s is %d years old",name,age));
 }
}
On the package you create interfaces IAskNameView com.jpeckham.examples.views. purpose of this interface is to make the defining view of the application later. and will be implemented in the main activity
package com.jpeckham.examples.views;

public interface IAskNameView {
 public void setDisplayedText(String text);
}


And Your Layout modif like this :





android programming,programming for android,programming android apps,mvp pattern,design patterns mvp instructor led training class


Related Link:

1 komentar:

James mengatakan...

This tutorial is either incomplete (seems to end rather abruptly), or its just plain wrong.

The last part, with the view XML, it's completely incorrect, you have formatting issues, typos, and nested view elements which are all invalid.

Was this an attempt to rip the post from James Peckham?

Posting Komentar