I am learning MVC and Android. I have create a sample program which is supposed to implement the MVC philosophy. I plan to build my codes on top of this.
I wish to know whether I have got the concept more or less, or if there are any fundamental issues in my understanding of the MVC.
Summary: This application will get a city name and display it with a few concatenations (country name etc).
Model -> Model.java
/**
*
*/
package abc.samples.mvc;
/**
* @author Rahul
*
*/
public class Model {
String mData;
String mInput;
String mOutput;
public void set(String temp) {
mInput = temp;
mData = mInput + ", India.";
}
public String get() {
mOutput = "You are from "+mData;
return mOutput;
}
}
View -> view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/vControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Control" />
<EditText
android:id="@+id/vInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/vControl"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/vOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/vInput"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Controller -> Controller.java
package abc.samples.mvc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Controller extends Activity {
EditText cInput;
TextView cOutput;
Button cControl;
Model cData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
cInput = (EditText) findViewById(R.id.vInput);
cOutput = (TextView) findViewById(R.id.vOutput);
cControl = (Button) findViewById(R.id.vControl);
cControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cData = new Model();
cData.set(cInput.getText().toString());
cOutput.setText(cData.get());
}
});
}
}