I've encountered a problem where I'm trying to pass a some information through to another activity, but the arraylist i'm using to store the data is returning null for the unique identifier i'm using to define each arraylist entry.
This is the class:
import java.util.ArrayList;
public class NutritionLessons {
public NutritionLessons(String lessonName, String lessonCode, String lessonCategory, String lessonContent){
this.lessonName = lessonName;
this.lessonCode = lessonCode;
this.lessonCategory = lessonCategory;
this.lessonContent = lessonContent;
}
private String lessonName;
private String lessonCode;
private String lessonCategory;
private String lessonContent;
public String getLessonName() {
return lessonName;
}
public void setLessonName(String lessonName) {
this.lessonName = lessonName;
}
public String getLessonCode() {
return lessonCode;
}
public void setLessonCode(String lessonCode) {
this.lessonCode = lessonCode;
}
public String getLessonCategory() {
return lessonCategory;
}
public void setLessonCategory(String lessonCategory) {
this.lessonCategory = lessonCategory;
}
public String getLessonContent() {
return lessonContent;
}
public void setLessonContent(String lessonContent) {
this.lessonContent = lessonContent;
}
This is the arraylist in the class
public static ArrayList<NutritionLessons> getLessons(){
ArrayList<NutritionLessons> lessons = new ArrayList<>();
lessons.add(new NutritionLessons("Basic Food Groups", "eee", "Nutrition Essentials",
"blah"));
lessons.add(new NutritionLessons("Food", "abc", "abc", "abc"));
return lessons;
}
And this is the for-each loop i'm using the return the lesson information to another activity
public static NutritionLessons getLessons(String lessonCode){ //lessonCode: null
ArrayList<NutritionLessons> lessons = NutritionLessons.getLessons();
for(final NutritionLessons lesson : lessons){
if(lesson.getLessonCode().equals(lessonCode)){
return lesson;
}
}
return lessons.get(lessons.size()-1);
}
It says that the lessonCode is returning null when being passed through and i'm not sure why, any help would be greatly appreciated!
EDIT: Attaching the places i've called getLesson(String lessonCode) - the idea is to have the information in the arraylist in a recycler view, and a subsequent activity with the lessonContent
recyclerView code:
public class nutritionLessonsList extends AppCompatActivity {
RecyclerView mRecyclerView2;
private nutritionLessonsAdapter mAdapter2;
private RecyclerView.LayoutManager layoutManager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nutrition_lessons_list);
mRecyclerView2 = findViewById(R.id.recyclerView2);
mRecyclerView2.setHasFixedSize(true);
layoutManager2 = new LinearLayoutManager(this);
mRecyclerView2.setLayoutManager(layoutManager2);
nutritionLessonsAdapter.Listener listener = new nutritionLessonsAdapter.Listener(){
@Override
public void onClick(View view, String lessonCode) {
launchDetailActivity(lessonCode);
}
};
mAdapter2 = new nutritionLessonsAdapter(NutritionLessons.getLessons(), listener);
mRecyclerView2.setAdapter(mAdapter2);
}
private void launchDetailActivity(String message){
Intent intent = new Intent(nutritionLessonsList.this, nutritionLessonPage.class);
intent.putExtra(nutritionLessonPage.INTENT_MESSAGE, message);
startActivity(intent);
}
}
The subsequent lesson page code
public class nutritionLessonPage extends AppCompatActivity {
public static final String INTENT_MESSAGE = "au.edu.unsw.infs3634.gamifiedlearning.intent_message";
TextView mLessonName;
TextView mLesson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nutrition_lesson_page);
Intent intent = getIntent();
String lessonCode = intent.getStringExtra(INTENT_MESSAGE);
final NutritionLessons nutritionLessons = NutritionLessons.getLessons(lessonCode);
// //Instantiating the view objects
mLessonName = findViewById(R.id.tvLessonName2);
mLesson = findViewById(R.id.tvLesson);
// //Set the content value
mLessonName.setText(nutritionLessons.getLessonName());
mLesson.setText(nutritionLessons.getLessonContent());
}
}
I want to add that, when I click through the recyclerview into the lesson page, only the last element in the array is passed through (ie. lessons.add(new NutritionLessons("Food", "abc", "abc", "abc"));) regardless of which recyclerView row i click
getLessonshas a value of null? If so, the problem will be in the code calling that method - which you haven't shown us.lessonCodeis null ?getLessons(String lessonCode)can not return null because u handled both cases. How and where are you callinggetLessons(String lessonCode)? add it with question . Apart from this you should really avoid calling one Activity's method from another there are better ways to do it . Since its static so its working fine for now