android cursor.moveToNext()?
For clarity a complete example would be as follows which I trust is of interest. As code comments indicated we essentially iterate over database rows and then columns to form a table of data as per database.
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null,
null);
//if the cursor isnt null we will essentially iterate over rows and then columns
//to form a table of data as per database.
if (cursor != null) {
//more to the first row
cursor.moveToFirst();
//iterate over rows
for (int i = 0; i < cursor.getCount(); i++) {
//iterate over the columns
for(int j = 0; j < cursor.getColumnNames().length; j++){
//append the column value to the string builder and delimit by a pipe symbol
stringBuilder.append(cursor.getString(j) + "|");
}
//add a new line carriage return
stringBuilder.append("\n");
//move to the next row
cursor.moveToNext();
}
//close the cursor
cursor.close();
}
Is Android Cursor.moveToNext() Documentation Correct?
Verbatim from the API:
Returns:
whether the move succeeded.
So, it means that:
Cursor in first row -> moveToNext() -> cursor in second row -> there's no second row -> return false
If you want the details, go to the source: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/database/AbstractCursor.java#AbstractCursor.moveToNext%28%29
public final boolean moveToNext() {
return moveToPosition(mPos + 1);
}
public final boolean moveToPosition(int position) {
// Make sure position isn't past the end of the cursor
final int count = getCount();
if (position >= count) {
mPos = count;
return false;
}
Never entering in cursor.moveToNext() in Android SQLite
What you are doing with this:
String[] selectionArgs = {"'" + personCellString + "'"};
is searching for the string value of personCellString
enclosed inside single quotes and not for the actual value of the string personCellString
.
Remove the single quotes:
String[] selectionArgs = {personCellString};
You don't have to worry about the string representation of personCellString
in the final sql statement that will be constructed and executed which will contain them without explicitly setting them.
while (cursor.moveToNext()) runs only once in android studio
If you want to loop over the cursor result set again, you need to rewind the cursor back to beginning. Replace the
while (cursor.moveToNext()) {
...
}
with
if (cursor.moveToFirst()) {
do {
...
} while (cursor.moveToNext());
}
and remove the cursor.close()
as you're not yet done with your cursor.
Cursor.moveToNext error
You appear to be making a fairly large query: at least 1127 rows, and for all possible columns (despite the fact that you are only using two of them). And, during your work with that Cursor
, you are doing disk I/O and/or IPC back to the ContentProvider
, assuming that UsefulDocumentFile
is related to Android's DocumentFile
.
As Prakash notes, the Cursor
that you get back may contain only a subset of the information. As soon as you try advancing past that point, the Cursor
needs to go back to the data source and get the next window of results. I can see you running into this sort of problem if there has been a substantial change in the data while this work has been going on (e.g., there are now fewer than 1127 rows).
I suggest that you:
Constrain the columns that you get back to the subset that you need, and
Avoid the I/O during the loop (e.g., spin through the
Cursor
to build up anArrayList<Pair>
or something, close theCursor
, then iterate over the list)
Related Topics
Download Images Using Android Webview
Android Studio - Cannot Resolve Symbol 'Firebase'
How to Avoid Scientific Notation in Double
How Many Activities Vs Fragments
Android Fingerprint API for Time Attendance App
I Want Show Notification at 8:00 Am Everyday
Android: Change Tab Text Color Programmatically
How to Access Adb in Os X Through Terminal, "Command Not Found"
Get Android .Apk File Versionname or Versioncode Without Installing APK
Use of Context to Start Another Activity
Stackoverflow: Caused by Nested Views
Gridlayoutmanager - How to Auto Fit Columns
Android Listview Child View Setenabled() and Setclickable() Do Nothing
Android Studio - Processdebugresources Failed
Want to Load Desktop Version in My Webview Using Uastring
Android, How to Limit Width of Textview (And Add Three Dots at The End of Text)