-2

I need to iterate through a list of variable names that are stored in a text file, store each as a TextView, and call methods on each one. Is there any way to do this? I have tried using the code provided in Is there any way to loop though variable names?, but TextView is not a class.

    void populate2(){
        for(int i = 0; i < taskArray3.size(); i++){
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
            ArrayList<String> a = FileHelper.readData() // Gets list of variable names from text list stored in FileHelper

            TextView txt1-20? = new TextView(this); // want to now iterate through all names in the list, don't know how.
            final float scale = this.getResources().getDisplayMetrics().density;
            int pixels = (int) (49 * scale + 0.5f);
            ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, pixels);
            txt1.setLayoutParams(params);
            txt1.setText(taskArray3.get(i));
            txt1.setGravity(Gravity.CENTER);
            linearLayout.addView(txt1);
        }
    }
the_redcar
  • 129
  • 1
  • 1
  • 10
  • You can't do that with variable names. Instead, save your `TextView`s in some collection from which you can retrieve them later. If you want to retrieve them by name, then a `Map` would be appropriate; e.g., `val textViews = mutableMapOf()`. – Mike M. Aug 28 '19 at 13:57

1 Answers1

0

you should use the object type's (Text View) with the whole package name.

android.widget.TextView var1;
android.widget.TextView var2;