我有一個片段和一個java類。我想從FragmenrVRList片段訪問Generator類中的courseID變量。我試圖將其聲明爲靜態變量,但不允許。我該怎麼做?預先感謝您:)如何從另一個類訪問courseID變量?
Generator.java
public class Generator {
public static List<DObjectVrList> getData(){
int courseID;
List<DObjectVrList> courses = new ArrayList<>();
for (int i=1; i<=8;i++){
DObjectVrList course = new DObjectVrList();
course.setId(i);
courseID = course.getId();
switch (i){
case 1:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 2:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 3:
course.setTitleCourse("Course " +i);
course.setCourseDate("Today at 9 pm");
break;
case 4:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 5:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 6:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 7:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
case 8:
course.setTitleCourse("Course "+i);
course.setCourseDate("Today at 9 pm");
break;
}
courses.add(course);
}
return courses;
}
FragmentVRList.java
public class FragmentVRList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main,container,false);
View cardView = inflater.inflate(R.layout.card_vr,container,false);
Generator generator = new Generator();
RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
AdapterVRList adapter = new AdapterVRList(getActivity(), Generator.getData());
recyclerView.setAdapter(adapter);
recyclerView.setNestedScrollingEnabled(false);
switch (Generator.getData())
return rootView;
}
}