如何在android中循环TextView变量

时间:2016-02-23 09:43:54

标签: java android for-loop textview

我是Android的新手,如果有任何错误,请通知我。我在 LinkedList<String> list=new LinkedList<String>(); list.add("c"); list.add("a"); list.add("t"); System.out.println(list.toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\, ", "")); 文件

中有一段代码
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); } }); } 是一个接口,GetLectureCallBack返回lectureLocalDatabase.getDataBack()个对象。随着LectureDetailslecTime的值发生变化,for loopcourseId变量的值也会发生变化。从halltvLecture1tvLecture5tvHall1有10个TextView。我需要将tvHall5courseId值分别分配给halltvLecture1,将第二个值分配给tvHall1tvLecture2,依此类推。那么如何使tvHall2变量在同一个for循环中循环。提前致谢

3 个答案:

答案 0 :(得分:2)

使用 TextView 循环显示某些信息的数组是不好的做法。 更好地使用 ListView RecyclerView

您可以在官方网站或互联网上找到更多信息 https://developer.android.com/guide/topics/ui/layout/listview.html
https://developer.android.com/training/material/lists-cards.html

答案 1 :(得分:1)

您可以拥有两个包含TextView个对象的数组,然后循环它们。

catch是你的i变量是一个局部变量,因此无法在匿名方法中访问,除非它被定义为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);

                }
          });
}

答案 2 :(得分:0)

你可以用这个:

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

现在您可以轻松遍历数组