【问题标题】:Call functions on textView names stored in a text file对存储在文本文件中的 textView 名称调用函数
【发布时间】:2019-08-28 05:35:23
【问题描述】:

我需要遍历存储在文本文件中的变量名称列表,将每个变量名称存储为 TextView,并在每个变量名称上调用方法。有没有办法做到这一点?我试过使用Is there any way to loop though variable names?提供的代码,但是TextView不是一个类。

    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);
        }
    }

【问题讨论】:

  • 你不能用变量名来做到这一点。相反,请将您的 TextViews 保存在某个集合中,以便以后从中检索它们。如果您想按名称检索它们,那么 Map 将是合适的;例如,val textViews = mutableMapOf&lt;String, TextView&gt;().

标签: java android android-studio


【解决方案1】:

您应该使用对象类型 (Text View) 和整个包名。

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

【讨论】:

  • 变量名存储在文本文件中,所以我无法直接启动它们。
  • 你能告诉我变量名和读取它们并初始化它们的代码吗?
猜你喜欢
  • 1970-01-01
  • 2021-03-11
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多