我想通过for循环填充一些TextViews.这不是一个真正的代码示例,但我希望它足以让您了解我正在尝试做什么.我甚至不确定这是可能的,但我希望有人找到了办法.
TextView dataTV0 = (TextView) v.findViewById(R.id.dataTV0);
TextView dataTV1 = (TextView) v.findViewById(R.id.dataTV1);
TextView dataTV2 = (TextView) v.findViewById(R.id.dataTV2);
TextView dataTV3 = (TextView) v.findViewById(R.id.dataTV3);
TextView dataTV4 = (TextView) v.findViewById(R.id.dataTV4);
TextView dataTV5 = (TextView) v.findViewById(R.id.dataTV5);
String[] data; //This is acquired from another source
for (int i = 0; i < 6; i++){
(String.format("dataTV%d", i).setText(data[i]);
}
Run Code Online (Sandbox Code Playgroud)
我认为一个选择是 Resources.getIdentifier()
for (int i = 0; i < 6; i++){
TextView textView = (TextView) findViewById( getResources().getIdentifier(String.format("dataTV%d", i), "id", getPackageName() ) )
if(textView != null)
textView.setText(data[i]);
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
为了避免许多功能调用:
TextView textView;
Resources rresources = getResources();
String packageName = getPackageName();
for (int i = 0; i < 6; i++){
textView = (TextView) findViewById( resources.getIdentifier(String.format("dataTV%d", i), "id", packageName ) )
if(textView != null)
textView.setText(data[i]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |