Created
August 9, 2015 06:07
-
-
Save pskink/3029ef9cd5216a6e7d29 to your computer and use it in GitHub Desktop.
ColumnExtenderCursor
This file contains 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
package com.example.app.myapplication; | |
import android.database.Cursor; | |
import android.database.CursorWrapper; | |
public class ColumnExtenderCursor extends CursorWrapper { | |
private final String[] columnNames; | |
public ColumnExtenderCursor(Cursor cursor, String... newColumns) { | |
super(cursor); | |
String[] oldColumnNames = cursor.getColumnNames(); | |
int oldColumnCount = cursor.getColumnCount(); | |
columnNames = new String[oldColumnCount + newColumns.length]; | |
System.arraycopy(oldColumnNames, 0, columnNames, 0, oldColumnNames.length); | |
System.arraycopy(newColumns, 0, columnNames, oldColumnCount, newColumns.length); | |
} | |
@Override | |
public int getColumnCount() { | |
return columnNames.length; | |
} | |
@Override | |
public int getColumnIndex(String columnName) { | |
for (int i = 0; i < columnNames.length; i++) { | |
if (columnNames[i].equals(columnName)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
@Override | |
public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { | |
int idx = getColumnIndex(columnName); | |
if (idx >= 0) { | |
return idx; | |
} | |
throw new IllegalArgumentException("column '" + columnName + "' does not exist"); | |
} | |
@Override | |
public String getColumnName(int columnIndex) { | |
return columnNames[columnIndex]; | |
} | |
@Override | |
public String[] getColumnNames() { | |
return columnNames; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment