簡體   English   中英

變量在數組列表中返回空值

[英]variable returning a null in arraylist

我遇到了一個問題,我試圖將一些信息傳遞給另一個活動,但是我用來存儲數據的 arraylist 為我用來定義每個 arraylist 條目的唯一標識符返回 null。

這是課程:

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;
    }

這是類中的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;

}

這是我使用的 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);
    }

它說,在通過時,課程代碼返回 null,我不知道為什么,任何幫助將不勝感激!

編輯:附上我稱為 getLesson(String courseCode) 的地方 - 想法是在回收器視圖中包含數組列表中的信息,以及隨后的帶有課程內容的活動

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);
    }
}

后續課程頁面代碼

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());

    }
}

我想補充一點,當我點擊 recyclerview 進入課程頁面時,只有數組中的最后一個元素被傳遞(即。 abc"));) 無論我點擊哪個 recyclerView 行

這是關於您最后一次聲明“無論我單擊哪個 recyclerView 行”

那是因為您在調用 getLessons() 時傳遞的課程代碼是不同的,並且不存在於您的數組列表中,並且當課程代碼不匹配時您已經處理了這種情況,然后返回最后一個元素 -

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

您列表中的最后一個元素是 -

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

如果我誤解了您的問題,希望我理解正確或糾正我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM