Giter Site home page Giter Site logo

androidmvcframework's Introduction

AndroidMVCFramework photo mvc_android.png

This is equivalent with MVC. This framework has been used by a lot of android projects. And this repository contains example of framework MVC for android.

Please feel free to give feedback for this framework, or if you want more discussion for improvement, you can reach me at [email protected].

##How to use?

View is defined by xml layout and activity. View should be cleared from the core/main processing. It just contains view definition, eventcallback or listener.

public class ActivityMain extends Activity {

    private Handler handler;
    private ControllerMain controller;
    private Button btnExecuteFib;
    private EditText etFibonaciSize;
    private TextView tvResultFib;

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

        handler = new HandlerMain(this);
        controller = new ControllerMain(handler, this);

        etFibonaciSize = (EditText)findViewById(R.id.et_fibonaci);
        btnExecuteFib = (Button)findViewById(R.id.bt_fibonaci);

        btnExecuteFib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controller.findFibonacciNumber(Integer.parseInt(etFibonaciSize.getText().toString()));
            }
        });

        tvResultFib = (TextView)findViewById(R.id.tv_fibonaci_result);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Debug.stopMethodTracing();
    }

    public TextView getTvResultFib(){
        return tvResultFib;
    }
}

Controller is defined by controller itself. Controller can be drived by view or if it is possible more to modul.

public class ControllerMain {

    private Handler handler;
    private Activity activity;

    public ControllerMain(Handler handler){
        this.handler = handler;
    }

    public ControllerMain(Handler handler, Activity activity){
        this.handler = handler;
        this.activity = activity;
    }

    public void findFibonacciNumber(int fibonacciSize){
        Fibonacci fibonacci = new Fibonacci(fibonacciSize);
        for(int i=0; i<fibonacciSize; i++){
            fibonacci.add(fibonacciRecursive(i));
        }

        Message msg = new Message();
        msg.what = HandlerMain.MAIN_FIBONACI;
        msg.obj = fibonacci;
        getHandler().sendMessage(msg);
    }

    private int fibonacciRecursive(int number){

        if (number==0){
            return 0;
        }

        if ((number==1)||(number==2)){
            return 1;
        }

        return fibonacciRecursive(number-1)+fibonacciRecursive(number-2);
    }

    public Activity getActivity(){
        return this.activity;
    }

    public Handler getHandler(){
        return this.handler;
    }
}

Once controller has done its job, to update the view, controller has to send message to handler. Handler is UI handle to update all UI component that need to update.

public class HandlerMain extends Handler {
    public static final int MAIN_FIBONACI = 0;
    public static final int MAIN_OTHER = 1;

    private ActivityMain activity;

    public HandlerMain(ActivityMain activity){
        this.activity = activity;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case MAIN_FIBONACI:
                displayFibonaci((Fibonacci) msg.obj);
                break;
            default:
                break;
        }
    }

    private void displayFibonaci(Fibonacci fibonacci){
        activity.getTvResultFib().setText(fibonacci.toString());
    }
}

For model, actually it could be from content provider, POJO or database helper.

public class Fibonacci {
    private int fibonacciCounter = 0;
    private int[] fibonacciNumbers;
    private int fibonacciSize;

    public Fibonacci(int fibonacciSize){
        this.fibonacciSize = fibonacciSize;
        this.fibonacciNumbers = new int[fibonacciSize];
    }

    public int size(){
        return fibonacciSize;
    }

    public void add(int fibonacciNumber){
        fibonacciNumbers[fibonacciCounter] = fibonacciNumber;
        fibonacciCounter++;
    }

    @Override
    public String toString() {
        String result = "";
        for (int i =0; i<size(); i++ ){
            result += fibonacciNumbers[i] + " ";
        }

        return result;
    }
}

androidmvcframework's People

Contributors

faren avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.