Android Studio how to check if specific list activity is clicked in another activity?

64 views Asked by At

My main activity is a list activity that pretty much lists the different terms for school. Once a term is clicked it will bring up a new page(activity) another list activity that shows all the courses in that term.

Is there a way where we can check if say term 1 or term 2 has been clicked on the main activity in the 2nd page/activity? For example my 2nd activity have different methods called populateTermX where X = term number. In the onCreate method it will do a check to see which term got clicked in the main activity and will call the populateTermX methods. That way I don't have to make like a bunch of different activities to correspond with the terms.

Code: Main Activity

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

    populateList();
}

private void populateList() {
    String[] terms = {// terms in here};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, terms);
    getListView().setAdapter(adapter);
}

Code: 2nd Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_course);
    if(term1 is clicked) {
       populateTerm1();
    } // etc...
}

private void populateTerm1() {
    String[] courses = {//courses in here};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, courses);
    getListView().setAdapter(adapter);
}

private void populateTerm2() {
    String[] courses = {//courses in here};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView.getContext(), android.R.layout.simple_list_item_1, courses);
    getListView().setAdapter(adapter);
}
0

There are 0 answers