简体   繁体   English

变量在数组列表中返回空值

[英]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.我遇到了一个问题,我试图将一些信息传递给另一个活动,但是我用来存储数据的 arraylist 为我用来定义每个 arraylist 条目的唯一标识符返回 null。

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这是类中的arraylist

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这是我使用的 for-each 循环将课程信息返回到另一个活动

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!它说,在通过时,课程代码返回 null,我不知道为什么,任何帮助将不胜感激!

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编辑:附上我称为 getLesson(String courseCode) 的地方 - 想法是在回收器视图中包含数组列表中的信息,以及随后的带有课程内容的活动

recyclerView code: recyclerView 代码:

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我想补充一点,当我点击 recyclerview 进入课程页面时,只有数组中的最后一个元素被传递(即。 abc"));) 无论我点击哪个 recyclerView 行

This is regarding your last statement that "regardless of which recyclerView row i click"这是关于您最后一次声明“无论我单击哪个 recyclerView 行”

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 -那是因为您在调用 getLessons() 时传递的课程代码是不同的,并且不存在于您的数组列表中,并且当课程代码不匹配时您已经处理了这种情况,然后返回最后一个元素 -

 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.如果我误解了您的问题,希望我理解正确或纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM