Last active
August 29, 2015 14:06
-
-
Save operando/0b5ff0bacbffecb0730a to your computer and use it in GitHub Desktop.
【Android】Cursorをいじるメソッドで気をつけたいPositionの話 ref: http://qiita.com/operandoOS/items/6527b30f7e9bd8b60200
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void dumpLastRecord(Cursor cursor) { | |
Log.d("dumpLastRecord", "============="); | |
if (cursor != null) { | |
cursor.moveToPosition(-1); | |
if (cursor.moveToLast()) { | |
String[] columnNames = cursor.getColumnNames(); | |
int length = columnNames.length; | |
for (int i = 0; i < length; i++) { | |
String value; | |
try { | |
value = cursor.getString(i); | |
} catch (SQLiteException e) { | |
value = "<unprintable>"; | |
} | |
Log.d("dumpLastRecord", columnNames[i] + " : " + value); | |
} | |
} | |
} | |
Log.d("dumpLastRecord", "============="); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cursor c = db.query(....); | |
c.moveToFirst(); | |
Log.d("Before Cursor Position","" + c.getPosition()); | |
dumpLastRecord(c); | |
Log.d("After Cursor Position","" + c.getPosition()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D/Bfter Cursor Position﹕ 0 | |
D/After Cursor Position﹕ 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void dumpLastRecord(Cursor cursor) { | |
Log.d("dumpLastRecord", "==========================="); | |
if (cursor != null) { | |
int startPosition = cursor.getPosition(); | |
cursor.moveToPosition(-1); | |
if (cursor.moveToLast()) { | |
String[] columnNames = cursor.getColumnNames(); | |
int length = columnNames.length; | |
for (int i = 0; i < length; i++) { | |
String value; | |
try { | |
value = cursor.getString(i); | |
} catch (SQLiteException e) { | |
value = "<unprintable>"; | |
} | |
Log.d("dumpLastRecord", columnNames[i] + " : " + value); | |
} | |
} | |
cursor.moveToPosition(startPosition); | |
} | |
Log.d("dumpLastRecord", "==========================="); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void dumpLastRecord(Cursor cursor) { | |
Log.d("dumpLastRecord", "==========================="); | |
if (cursor != null) { | |
int startPosition = cursor.getPosition(); // Cursorをいじる前に現在のPositionを一旦変数に保持 | |
// 省略 | |
cursor.moveToPosition(startPosition); // Cursorをいじり終わったら元のPositionに戻してあげる | |
} | |
Log.d("dumpLastRecord", "==========================="); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D/Bfter Cursor Position﹕ 0 | |
D/After Cursor Position﹕ 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment