cluelessstudent101
cluelessstudent101

Reputation: 1

variable returning a null in arraylist

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

Upvotes: 0

Views: 55

Answers (1)

Anita
Anita

Reputation: 93

This is regarding your last statement that "regardless of which recyclerView row i click"

That's because the lessonCode that you're passing while calling getLessons() is different and is not present in your arraylist and you've handled that case when lessonCode doesn't match then return the last element -

 return lessons.get(lessons.size()-1);

And the last element in your list is this -

new NutritionLessons("Food", "abc", "abc", "abc")

Hope I understood it correctly or correct me if I misunderstood your question.

Upvotes: 1

Related Questions