Skip to content

Commit 87ab696

Browse files
author
Hung-ying Tyan
committed
Fix crash when SimpleCursorAdapter changes cursor from null
when the spinner's drop-down view is shown. Bug: 6637141 Change-Id: I62f759f778b040db386cc72a753c3b2745517b7e
1 parent 5bb835a commit 87ab696

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

core/java/android/widget/SimpleCursorAdapter.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,
7878
super(context, layout, c);
7979
mTo = to;
8080
mOriginalFrom = from;
81-
findColumns(from);
81+
findColumns(c, from);
8282
}
8383

8484
/**
@@ -104,7 +104,7 @@ public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,
104104
super(context, layout, c, flags);
105105
mTo = to;
106106
mOriginalFrom = from;
107-
findColumns(from);
107+
findColumns(c, from);
108108
}
109109

110110
/**
@@ -316,20 +316,21 @@ public CharSequence convertToString(Cursor cursor) {
316316
}
317317

318318
/**
319-
* Create a map from an array of strings to an array of column-id integers in mCursor.
320-
* If mCursor is null, the array will be discarded.
321-
*
319+
* Create a map from an array of strings to an array of column-id integers in cursor c.
320+
* If c is null, the array will be discarded.
321+
*
322+
* @param c the cursor to find the columns from
322323
* @param from the Strings naming the columns of interest
323324
*/
324-
private void findColumns(String[] from) {
325-
if (mCursor != null) {
325+
private void findColumns(Cursor c, String[] from) {
326+
if (c != null) {
326327
int i;
327328
int count = from.length;
328329
if (mFrom == null || mFrom.length != count) {
329330
mFrom = new int[count];
330331
}
331332
for (i = 0; i < count; i++) {
332-
mFrom[i] = mCursor.getColumnIndexOrThrow(from[i]);
333+
mFrom[i] = c.getColumnIndexOrThrow(from[i]);
333334
}
334335
} else {
335336
mFrom = null;
@@ -341,13 +342,8 @@ public Cursor swapCursor(Cursor c) {
341342
// super.swapCursor() will notify observers before we have
342343
// a valid mapping, make sure we have a mapping before this
343344
// happens
344-
if (mFrom == null) {
345-
findColumns(mOriginalFrom);
346-
}
347-
Cursor res = super.swapCursor(c);
348-
// rescan columns in case cursor layout is different
349-
findColumns(mOriginalFrom);
350-
return res;
345+
findColumns(c, mOriginalFrom);
346+
return super.swapCursor(c);
351347
}
352348

353349
/**
@@ -367,11 +363,8 @@ public void changeCursorAndColumns(Cursor c, String[] from, int[] to) {
367363
// super.changeCursor() will notify observers before we have
368364
// a valid mapping, make sure we have a mapping before this
369365
// happens
370-
if (mFrom == null) {
371-
findColumns(mOriginalFrom);
372-
}
366+
findColumns(c, mOriginalFrom);
373367
super.changeCursor(c);
374-
findColumns(mOriginalFrom);
375368
}
376369

377370
/**

0 commit comments

Comments
 (0)