2017-08-20 70 views
-2

我有一個片段和一個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; 
} 

}

回答

0

你在靜態函數中定義了courseID。這意味着該變量的範圍僅限於該函數 - 除非該函數被調用,否則它不存在。

我猜你是一個很新的程序員?因爲這段代碼的風格需要很多工作。首先,你根本不需要這個switch語句。每個分支都做同樣的事情。整個循環可改寫爲:

for (int i=1; i<=8;i++){ 
    DObjectVrList course = new DObjectVrList(); 
    course.setId(i); 
    course.setTitleCourse("Course "+i); 
    course.setCourseDate("Today at 9 pm"); 
    courses.add(course); 
} 

一般來說,如果你發現自己的循環和循環變量做一個開關,你幾乎總是錯誤的。實際上,我也看到你後來也要轉換語句 - switch語句實際上並沒有在你使用它們那麼近的地方使用,你需要花更多時間去思考你是如何操縱泛型數據的,而不是對個人價值做出假設。

然後,當你想要得到的課程ID,你不需要在你的生成類來訪問任何東西。你剛纔去

List<DObjectVrList> courses = Generator.getData(); 
int courseID = courses.get(index_you_want).getCourseId(); 

如果你想要做的事,以所有課程,你去:

for(DObjectVrList course : courses) { 
    int courseID = course.getCourseID(); 
    //DO whatever you want with the course id here- without using a switch 
} 
0

首先,courseID不是Generator類的成員;它是getData()函數的局部變量。

課程ID似乎從DObjectVrList.getId()檢索。由於getData()返回DObjectVrList對象的列表,你可以遍歷列表,並得到每個DObjectVrList過程ID使用getId()列表。

Generator generator = new Generator(); 
List<DObjectVrList> data = Generator.getData(); 

for (DObjectVrList course : data) { 
    int courseID = course.getId(); 
}