Android TextViews. Is parameterization possible? Is it possible to snap to a model?

I am new to Android and Stack Overflow. I started developing an Android application and I am interested in two things:

1) Is it possible to parameterize a TextView? Suppose I want to display a text message that says something like: "User age is 38." Assume that the age of the user is the result of an algorithm. Using some typical i18n infrastructure, I would write something like "User age - {0}" in my i18n file. Then at runtime I would populate the parameters accordingly. I was not able to figure out how to do this or a similar approach in Android.

2) Suppose I have a complex object with many fields. For example: PersonModel, which has an id, name, age, country, favorite video game, whatever. If I want all this information to be displayed in one layout in one of my actions, the only way I found is to get all the necessary text elements by identifier, and then fill them one by one through the code. I was wondering if there is any binding / snapping mechanism in which I can execute something like: render (myPerson, myView), and this automatically reflects each property of the model into each of the TextView. If someone has ever worked with SpringMVC, Im looking for something similar to their mechanism for mapping objects / object models to views (e.g. spring: forms).

Thank you very much for your help. Hope this is useful for someone else =) Goodbye!

+3
source share
1 answer

In response to # 1: you want String.format () . This will allow you to do something like:

int age = 38;
String ageMessage = "The user age is %d";
myTextView.setText(String.format(ageMessage, age));

These two that you will use most %dfor numbers and %sfor strings. It uses the printf format if you know it, if you don't have a quick guide in Formatter docs.

For # 2, I think you are doing it best (grab the grab and fill them in manually). If you meet anything else, I would love to see it.

+2
source

Source: https://habr.com/ru/post/1732464/


All Articles