【问题标题】:while (cursor.moveToNext()) runs only once in android studiowhile (cursor.moveToNext()) 在 android studio 中只运行一次
【发布时间】:2021-03-11 02:02:34
【问题描述】:

我想在单击每个日期时显示不同的文本,我检查了日志,似乎当我单击一次后语句不起作用..有什么想法吗?

这是我的代码

     calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
                tapDate = year + "/" + (month + 1) + "/" + dayOfMonth;
                selectedDate.setText(tapDate);
                Log.d(TAG, "onSelectedDayChange: tapDate"+ tapDate);

                while (cursor.moveToNext()) {
                    item = new Shelf_items();
                    item.setBookTitle(cursor.getString(0));
                    item.setWriteDate(cursor.getString(1));
                    Log.d(TAG, "onSelectedDayChange: while runs");

                    if(item.getWriteDate().equals(tapDate)){
                        mShelf.add(item);
                        Log.d(TAG, "onSelectedDayChange: mshelf item"+item);

                        for(Shelf_items i: mShelf){
                            i_result = i.getBookTitle() + " / ";
                        }
                        f_result +=i_result;
                        Log.d(TAG, "onSelectedDayChange: for 문: "+i_result);
                    }
                    bookName.setText(f_result);
                }
                cursor.close();
                
            }

        });

【问题讨论】:

    标签: calendar cursor android-sqlite android-cursor


    【解决方案1】:

    如果要再次循环游标结果集,则需要将游标倒回到开头。替换

    while (cursor.moveToNext()) {
        ...
    }
    

    if (cursor.moveToFirst()) {
        do {
            ...
        } while (cursor.moveToNext());
    }
    

    并删除 cursor.close(),因为您的光标尚未完成。

    【讨论】:

    • 我不得不调整一些东西,但谢谢它有效!现在我知道如果我想循环,必须移动光标!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多