pjamesrud

pjamesrud

Android app, using a loop value to set visibilty and add data to textview?

I am creating an app that allows user to enter values and depending on the value a number of textviews are changed programmatically from gone to visible.
In the xml file there is TV!,TV2 and TV3, The user enters a couple of numbers from previous activity and these are used to set the visibility of the TV1,TV2 and TV3 textviews

Example:
            User enters 2.
Using a for loop 
        for (int i = 0; i<= userinput; i++){
          showTV = "TV"+(i);
          showTV.setVisibility(View.VISIBLE);
          showTV.setText(String.valueOf(list1.get(1)));

But I can not get the textview’s Id to be recognized using the i value

Can it be done this way or is this the wrong direction.

First Post!

Veeran

Veeran

It seems like you’re on the right track, but there’s a small mistake in your approach. When trying to dynamically set the visibility of TextViews based on user input, you should use the resource identifier (R.id) to access the TextViews rather than concatenating a string.

You can achieve this by creating an array of TextViews in your Java code that corresponds to the TextViews in your XML layout. Then, you can iterate through this array to set the visibility of TextViews based on the user input. Here’s a simplified example:

// Assuming you have TextViews in your XML with IDs TV1, TV2, and TV3
TextView tvArray = new TextView{findViewById(R.id.TV1), findViewById(R.id.TV2), findViewById(R.id.TV3)};

int userInput = 2; // Example user input

for (int i = 0; i <= userInput; i++) {
tvArray[i].setVisibility(View.VISIBLE);
tvArray[i].setText(String.valueOf(list1.get(i))); // Assuming list1 contains the values you want to set
}

Best Regards

Where Next?

Popular Frontend topics Top

davearonson
I’m unit-testing some JS, with Jasmine, and I’d like to check our coverage. We’re not using any front-end framework, nor much JS, so no ...
New
axelson
In Elixir I love to use the library GitHub - sasa1977/boundary: Manage and restrain cross-module dependencies in Elixir projects to enfor...
New
david-j-m
Hi, have a svelte spa gallery site - repl here. Have a couple of category buttons - Oil on Canvas and WaterColor… Sidebar contains all im...
New
AlessandroDsgroup
Hi to everyone, we are experiencing a 401 error related to the connection of the websocket (in reference to our web app); we are unable ...
New
sona11
I have a 1D array of numbers and need help splitting them into groups using a jagged array so that I can perform a series of computations...
New
harwind
First have a look at the code: function mainfunc(func, par3, par2){ window[func](par3, par2); } function calledfunc(par3, par2){ ...
/js
New
Fl4m3Ph03n1x
Background I have Phoenix umbrella application. When inside said application, I can run it without issues if MIX_ENV=prod. However, if I ...
New
gameengineer
We are developing on Samsung Tab Active 4 Pro using Android Studio, kotlin and java. We are getting what we think are app deadlocks. The ...
New
pjamesrud
I am creating an app that allows user to enter values and depending on the value a number of textviews are changed programmatically from ...
New
ramiro-marinio
Hello. So unexperienced frontend dev here. Basically, 3 or 4 months ago I started working on a project and I committed the capital sin of...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New