Java 如何在android中循环使用TextView变量

Java 如何在android中循环使用TextView变量,java,android,for-loop,textview,Java,Android,For Loop,Textview,我对Android非常陌生,如果有任何错误,请通知我。我的java文件中有这段代码 String temp1=tvLecTime1.getText().toString(); String temp2=tvLecTime2.getText().toString(); String temp3=tvLecTime3.getText().toString(); String temp4=tvLecTime4.getText().toString(); String temp5=tvLecTime5.

我对Android非常陌生,如果有任何错误,请通知我。我的
java
文件中有这段代码

String temp1=tvLecTime1.getText().toString();
String temp2=tvLecTime2.getText().toString();
String temp3=tvLecTime3.getText().toString();
String temp4=tvLecTime4.getText().toString();
String temp5=tvLecTime5.getText().toString();
String temp[]={"null",temp1,temp2,temp3,temp4,temp5};

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

    lecTime=temp[i];
    requestServerForTimeTable.fetchLectureDataInBackgroundEditTimeTable(
           day, year, lecTime,
           new GetLectureCallBack() {

                @Override
                public void done(LectureDetails lectureDetailsReturned) {

                    lectureLocalDatabase.storeData(lectureDetailsReturned);
                    LectureDetails lectureDetailsReceivedFromSP = 
                                          lectureLocalDatabase.getDataBack();
                    tvLecture1.setText(lectureDetailsReceivedFromSP.courseId);
                    tvHall1.setText(lectureDetailsReceivedFromSP.hall);

                }
          });
}
String temp1=tvLecTime1.getText().toString();
字符串temp2=tvLecTime2.getText().toString();
字符串temp3=tvLecTime3.getText().toString();
字符串temp4=tvLecTime4.getText().toString();
字符串temp5=tvLecTime5.getText().toString();
字符串temp[]={“null”,temp1,temp2,temp3,temp4,temp5};
对于(int i=1;i,您可以使用:

TextView[] tvLectViews = {tvLecture1, tvLecture2, tvLecture3, tvLecture4, tvLecture5};
TextView[] tvHallViews = {tvHall1, tvHall2, tvHall3, tvHall4, tvHall5};
现在,您可以轻松地遍历数组

您可以使用以下方法:

TextView[] tvLectViews = {tvLecture1, tvLecture2, tvLecture3, tvLecture4, tvLecture5};
TextView[] tvHallViews = {tvHall1, tvHall2, tvHall3, tvHall4, tvHall5};

现在,您可以轻松地遍历数组

您可以使用两个数组来保存
文本视图
对象,然后在其上循环

catch是您的
i
变量是一个局部变量,因此不能在匿名方法中访问,除非它被定义为
final
(您不能这样做,因为您需要增加它)。解决方案是将它传递给一个新的final变量:

String temp1=tvLecTime1.getText().toString();
String temp2=tvLecTime2.getText().toString();
String temp3=tvLecTime3.getText().toString();
String temp4=tvLecTime4.getText().toString();
String temp5=tvLecTime5.getText().toString();
String temp[]={temp1,temp2,temp3,temp4,temp5};

TextView[] tcLectures = { tvLecture1, tvLecture2, tvLecture3, tvLecture4, tvLecture5 };
TextView[] tvHalls = { tvHall1, tvHall2, tvHall3, tvHall4, tvHall5 };

for(int i=0; i<5; i++){

    lecTime=temp[i];
    final currentIndex = i;

    requestServerForTimeTable.fetchLectureDataInBackgroundEditTimeTable(
           day, year, lecTime,
           new GetLectureCallBack() {

                @Override
                public void done(LectureDetails lectureDetailsReturned) {

                    lectureLocalDatabase.storeData(lectureDetailsReturned);
                    LectureDetails lectureDetailsReceivedFromSP = 
                                          lectureLocalDatabase.getDataBack();
                    tcLectures[currentIndex].setText(
                              lectureDetailsReceivedFromSP.courseId);
                    tvHalls[currentIndex].setText(
                              lectureDetailsReceivedFromSP.hall);

                }
          });
}
String temp1=tvLecTime1.getText().toString();
字符串temp2=tvLecTime2.getText().toString();
字符串temp3=tvLecTime3.getText().toString();
字符串temp4=tvLecTime4.getText().toString();
字符串temp5=tvLecTime5.getText().toString();
字符串temp[]={temp1,temp2,temp3,temp4,temp5};
TextView[]tc体系结构={tv讲师1、tv讲师2、tv讲师3、tv讲师4、tv讲师5};
TextView[]tvHalls={tvHall1,tvHall2,tvHall3,tvHall4,tvHall5};

对于(int i=0;i您可以使用两个数组来保存
TextView
对象,然后在它们上面循环

catch是您的
i
变量是一个局部变量,因此不能在匿名方法中访问,除非它被定义为
final
(您不能这样做,因为您需要增加它)。解决方案是将它传递给一个新的final变量:

String temp1=tvLecTime1.getText().toString();
String temp2=tvLecTime2.getText().toString();
String temp3=tvLecTime3.getText().toString();
String temp4=tvLecTime4.getText().toString();
String temp5=tvLecTime5.getText().toString();
String temp[]={temp1,temp2,temp3,temp4,temp5};

TextView[] tcLectures = { tvLecture1, tvLecture2, tvLecture3, tvLecture4, tvLecture5 };
TextView[] tvHalls = { tvHall1, tvHall2, tvHall3, tvHall4, tvHall5 };

for(int i=0; i<5; i++){

    lecTime=temp[i];
    final currentIndex = i;

    requestServerForTimeTable.fetchLectureDataInBackgroundEditTimeTable(
           day, year, lecTime,
           new GetLectureCallBack() {

                @Override
                public void done(LectureDetails lectureDetailsReturned) {

                    lectureLocalDatabase.storeData(lectureDetailsReturned);
                    LectureDetails lectureDetailsReceivedFromSP = 
                                          lectureLocalDatabase.getDataBack();
                    tcLectures[currentIndex].setText(
                              lectureDetailsReceivedFromSP.courseId);
                    tvHalls[currentIndex].setText(
                              lectureDetailsReceivedFromSP.hall);

                }
          });
}
String temp1=tvLecTime1.getText().toString();
字符串temp2=tvLecTime2.getText().toString();
字符串temp3=tvLecTime3.getText().toString();
字符串temp4=tvLecTime4.getText().toString();
字符串temp5=tvLecTime5.getText().toString();
字符串temp[]={temp1,temp2,temp3,temp4,temp5};
TextView[]tc体系结构={tv讲师1、tv讲师2、tv讲师3、tv讲师4、tv讲师5};
TextView[]tvHalls={tvHall1,tvHall2,tvHall3,tvHall4,tvHall5};

对于(inti=0;i,使用TextView的循环来显示一些信息的数组是不好的做法。 更好地使用列表视图回收视图

您可以在官方网站或互联网上找到更多信息。


使用文本视图的循环来显示一些信息的数组是一种不好的做法。 更好地使用列表视图回收视图

您可以在官方网站或互联网上找到更多信息。


您可以为
tvHall
tvteaching
制作
TextView
数组,或者您可以将ListView与自定义适配器一起使用。您可以为
tvHall
tvteaching
制作
文本视图数组,或者您可以将ListView与自定义适配器一起使用。我会测试它,并尽快让您知道它是如何运行的,right现在我正在修复运行此代码之前出现的一个错误:(我将测试它并尽快让您知道它是如何运行的,现在我正在修复运行此代码之前出现的一个错误:(