Android TextView Update

I have a simple Android application that I want to write on the display the value of one field belonging to another class.

Using a simple text view, I can write the initial value of the field, but I do not know how to update the text on the display whenever the field changes the value.

Btw, this is my first Android app that I still lost

Here is my activity code:

public class findIT extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PositionHolder ph = new PositionHolder(); TextView tv = new TextView(this); setContentView(tv); //this.updateText(); tv.setText("You are at " + ph.getPosition()); } } 
+7
source share
3 answers

You need to create a TextView in the xml layout as follows:

 <TextView android:id="@id/textView01" android:text="Enter your initial text " android:layout_width="wrap content" android:layout_height="wrap content" ></TextView> 

Then write the following in your class after the setContentView () function:

 TextView textView = (TextView) findViewById(R.id.textView01); textView.setText("Enter whatever you Like!"); 
+11
source

Chris .... Here is the easiest way to develop an application. Divide the problem into three parts. Presentation or presentation. Algorithm or model. A controller that responds to user and system events. This creates a “separation of concerns” so that the Controller owns the view and model. You create a view using xml, as in main.xml. You create a separate class to do the work, say MyModel.java, and of course there is a controller or an Activity class, say MyActivity.java. Thus, the data comes from a model entering the controller, which updates the view.

So your question is how to get data from the model and update the view. Naturally, this will happen in the controller, in your activity. The easiest way to do this is to put the button in action, and when the user clicks the button, call model.getLatestData () and update the view. This is PULLing data. The next way is to ensure that the Controller checks for updates every minute. This is POLLING for data. The next way is for the Controller to register interest in the model changes and wait for the model to report the change and then update the view. This is an asynchronous transfer of data from the model to the controller and can be performed using the OBSERVER template.

I know that this makes no sense to you when you struggle with trying to just get the code to work, but I hope I put the seed of an idea in your head that will bother you and make sense someday in the future.

Jal

+3
source

You will need to define the layout in xml, and then when you create the TextView, you bind it to its layoutID. Something like:

 TextView tv = (TextView) findViewById(R.id.something); 

I can explain a little more if you need to, but it will give you a starting place to search for additional answers.

0
source

All Articles