while (cursor.moveToNext()) 在 android studio 中只运行一次
while (cursor.moveToNext()) runs only once in android studio
我想在点击每个日期时显示不同的文本,我检查了日志,似乎在我点击一次后 while 语句不起作用..有什么想法吗?
这是我的代码
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();
}
});
如果要再次遍历游标结果集,需要将游标倒回到开头。替换
while (cursor.moveToNext()) {
...
}
与
if (cursor.moveToFirst()) {
do {
...
} while (cursor.moveToNext());
}
并删除 cursor.close()
,因为您还没有完成光标。
我想在点击每个日期时显示不同的文本,我检查了日志,似乎在我点击一次后 while 语句不起作用..有什么想法吗?
这是我的代码
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();
}
});
如果要再次遍历游标结果集,需要将游标倒回到开头。替换
while (cursor.moveToNext()) {
...
}
与
if (cursor.moveToFirst()) {
do {
...
} while (cursor.moveToNext());
}
并删除 cursor.close()
,因为您还没有完成光标。