如果您对 R 文件保持大腿控制以确保您的 ID 是连续的,您可以尝试以下操作:
for (int i = R.id.firstTextView; i <= R.id.lastTextView; i++){
views.setTextViewText(i, "" + realtimeData.get(i).id);
}
这将允许只将 ID 添加到 R 文件中,而无需修改代码来更改硬编码的6
数字。
只是为了证明我的观点:
<TextView android:id="@+id/testStart" />
<TextView android:id="@+id/test2" />
<TextView android:id="@+id/test3" />
<TextView android:id="@+id/testEnd" />
<Button android:id="@+id/button1" />
生成:
public static final int button1=0x7f070004;
public static final int menu_settings=0x7f070005;
public static final int test2=0x7f070001;
public static final int test3=0x7f070002;
public static final int testEnd=0x7f070003;
public static final int testStart=0x7f070000;
后期添加:
<TextView android:id="@+id/test4" />
就在之前
<TextView android:id="@+id/testEnd" />
立即将 R.java 更改为:
public static final int button1=0x7f070005;
public static final int menu_settings=0x7f070006;
public static final int test2=0x7f070001;
public static final int test3=0x7f070002;
public static final int test4=0x7f070003;
public static final int testEnd=0x7f070004;
public static final int testStart=0x7f070000;