0

I need to exchange data between activity and it's layout xml in Android. But I do not find a way to do this in Android. For example, views and controller in mvc pattern always has a way to exchange data between. So I am wondering is there any way to exchange data between them to should I refresh my mind and realize there is no such way?

ssj
  • 1,737
  • 2
  • 16
  • 28

4 Answers4

0

use below code to get value from layout in your activity

 String value;
  EditText editText= (EditText) findViewById(R.id.editText);
 value=editText.getText();
Android dev
  • 273
  • 2
  • 5
  • 23
0

code in xml example:

<ImageView
  android:id="@+id/image_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

in java class (like onCreate()):

ImageView image = (ImageView) findViewById(R.id.image_view);

Then you can do what you want with image

Lunchbox
  • 1,538
  • 2
  • 16
  • 42
0

I believe you're unsure exactly what you're asking. If you want to exchange information, such as ID or text entered into a textfield then any good android tutorial should be-able to demonstrate this. Considering your last comment I think you're talking about GET and POST based technologies which can be done usingREST andSOAP, or both if you want. This questions answer has a good implementation and definition of what both of these webservices are.

P.S. If this is what you're looking for then upvote that answer.

Community
  • 1
  • 1
Katana24
  • 8,706
  • 19
  • 76
  • 118
0

As some additional info, the "view" (XML Layout file) gets set by your activity initially on your activity's onCreate method. Right after you call it's parent method (super.onCreate()).

To maintain scope throughout the activity I tend to declare all the layout widgets that I need to the activity to interact with outside of any methods and within the class.

TextView textWelcomeMessage;

public void MyActivity(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // before calling setContentView() we have the option to change 
    // window features ex requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_my_activity);

    // Now to set the textview
    textWelcomeMessage = (TextView) findViewById(R.id.textWelcomeMessage);

    // Set some data
    textWelcomeMessage.setText("Hello, welcome to my activity");
}

It's not exactly like traditional php style mvc since static typing changes thing up a bit and we have to worry about scope. But the core principles can still apply as far as data abstraction and separation go. Hope this helps =)

Patty P
  • 351
  • 2
  • 12