Kasun Siyambalapitiya
Kasun Siyambalapitiya

Reputation: 4393

how to loop through TextView variables in android

I am very new to Android, and If any errors exists please notify me. I have this code in one of my java file

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 is an interface and lectureLocalDatabase.getDataBack() returns an LectureDetails object. as the values for lecTime changes throughout the for loop the values for courseId and hall variables are also changed. there are 10 TextViews from tvLecture1 upto tvLecture5 and tvHall1 upto tvHall5. I need to assign first courseId and hall values to tvLecture1 and tvHall1 respectively and second values to tvLecture2 and tvHall2 and so on. So how make the TextView variables get looped in the same for loop. Thanks in advance

Upvotes: 0

Views: 1275

Answers (3)

Oleksandr
Oleksandr

Reputation: 902

It's bad practice using loop of TextView to display array of some information. Much better use ListView or RecyclerView:

You can find more information on offical site or Internet.
https://developer.android.com/guide/topics/ui/layout/listview.html
https://developer.android.com/training/material/lists-cards.html

Upvotes: 2

Ori Lentz
Ori Lentz

Reputation: 3688

You could have two Array holding the TextView objects and then loop over them.

The catch is your i variable is a local variable and therefore cannot be accessed inside the Anonymous Method unless it is defined as final (which you cannot do since you need to increment it). The solution would be to pass it to a new variable who is 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);

                }
          });
}

Upvotes: 1

Pooya
Pooya

Reputation: 6126

you can use this:

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

now you can easily iterate through the array

Upvotes: 0

Related Questions