我是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()
个对象。随着LectureDetails
中lecTime
的值发生变化,for loop
和courseId
变量的值也会发生变化。从hall
到tvLecture1
和tvLecture5
到tvHall1
有10个TextView。我需要将tvHall5
和courseId
值分别分配给hall
和tvLecture1
,将第二个值分配给tvHall1
和tvLecture2
,依此类推。那么如何使tvHall2
变量在同一个for循环中循环。提前致谢
答案 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};
现在您可以轻松遍历数组