Java 添加大量文本视图

Java 添加大量文本视图,java,android,loops,Java,Android,Loops,我想在代码中分配大量文本视图(总共272个): 关于如何做到这一点,有什么想法吗?使用固定ID且不将其静态键入为属性的唯一选项是包含对视图引用的TextView列表,如下所示: List<TextView> textviews = new ArrayList<>(); Resources res = getResources(); for (int i = 1; i <=272; i++) { int id = res.getIdentifier("re

我想在代码中分配大量文本视图(总共272个):


关于如何做到这一点,有什么想法吗?

使用固定ID且不将其静态键入为属性的唯一选项是包含对视图引用的TextView列表,如下所示:

List<TextView> textviews = new ArrayList<>();
Resources res = getResources();

for (int i = 1; i <=272; i++) {

    int id = res.getIdentifier("resul" + i, "id", getContext().getPackageName());
    TextView tv = (TextView) findViewById(id);
    textviews.add(tv);
}
List<TextView> results = new ArrayList<>();
for (int i = 1; i <= 272; i++) {
    TextView textView = (TextView) getIdByName("resul" + i);
    results.add(textView);
}

使用固定ID且不将其静态类型化为属性的唯一选项是包含对视图引用的TextView列表,如下所示:

List<TextView> textviews = new ArrayList<>();
Resources res = getResources();

for (int i = 1; i <=272; i++) {

    int id = res.getIdentifier("resul" + i, "id", getContext().getPackageName());
    TextView tv = (TextView) findViewById(id);
    textviews.add(tv);
}
List<TextView> results = new ArrayList<>();
for (int i = 1; i <= 272; i++) {
    TextView textView = (TextView) getIdByName("resul" + i);
    results.add(textView);
}

Android有API动态检索id常量(按名称):,它可以用于“批量生产”这些id。
为了存储结果,可以使用数组或集合


同样,以这种方式编写代码肯定会使您成为必须维护代码的人的敌人。一个更好的主意是三思而后行,并提出一些更好的设计。

Android有API来动态检索id常量(按名称):,它可以用于“批量生产”这些id。
为了存储结果,可以使用数组或集合


同样,以这种方式编写代码肯定会使您成为必须维护代码的人的敌人。一个更好的主意是三思而后行,想出一些更好的设计。

我假设你有272个
文本视图
,都有单独的id

您可以通过如下字符串获取int id

public static int getIdByName(String name) {
    return getResourceIdByName(name, "id");
}

public static int getResourceIdByName(String name, String type) {
    StringBuilder s = new StringBuilder(context.getPackageName()).append(":").append(type).append("/").append(name);
    return context.getResources().getIdentifier(s.toString(), null, null);
}
因此,对于您来说,您可以使用上面的方法,循环所有272个文本视图,并插入到
列表中,如下所示:

List<TextView> textviews = new ArrayList<>();
Resources res = getResources();

for (int i = 1; i <=272; i++) {

    int id = res.getIdentifier("resul" + i, "id", getContext().getPackageName());
    TextView tv = (TextView) findViewById(id);
    textviews.add(tv);
}
List<TextView> results = new ArrayList<>();
for (int i = 1; i <= 272; i++) {
    TextView textView = (TextView) getIdByName("resul" + i);
    results.add(textView);
}
List results=new ArrayList();

对于(inti=1;i我假设您有272个
文本视图
,所有视图都有单独的id

您可以通过如下字符串获取int id

public static int getIdByName(String name) {
    return getResourceIdByName(name, "id");
}

public static int getResourceIdByName(String name, String type) {
    StringBuilder s = new StringBuilder(context.getPackageName()).append(":").append(type).append("/").append(name);
    return context.getResources().getIdentifier(s.toString(), null, null);
}
因此,对于您来说,您可以使用上面的方法,循环所有272个文本视图,并插入到
列表中,如下所示:

List<TextView> textviews = new ArrayList<>();
Resources res = getResources();

for (int i = 1; i <=272; i++) {

    int id = res.getIdentifier("resul" + i, "id", getContext().getPackageName());
    TextView tv = (TextView) findViewById(id);
    textviews.add(tv);
}
List<TextView> results = new ArrayList<>();
for (int i = 1; i <= 272; i++) {
    TextView textView = (TextView) getIdByName("resul" + i);
    results.add(textView);
}
List results=new ArrayList();

对于(int i=1;i)您试图解决什么问题?视图是在xml布局中固定的(固定ID)还是在代码中动态创建的?不理解这个问题。当Result1是时,texx1从未在布局中使用过,如果这回答了这个问题。您试图解决什么问题?视图是固定的(固定ID)吗在xml布局内或在代码中动态创建?不理解这个问题。texx1从未在布局中使用,而Result1是,如果它回答了这个问题。这不是我答案的副本吗?我在你之前回答;-)嗯…20:17 vs 20:21?:)这不是我答案的副本吗?我在你之前回答;-)隐马尔可夫模型。。。20:17对20:21?:)