Created
March 26, 2012 03:03
-
-
Save tai2/2202589 to your computer and use it in GitHub Desktop.
Split one cursor into multiple columns. A single column cursor (a1, a2, a3, a4) split into a double column cursor ((a1, a2), (a3, a4)).
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
import android.database.AbstractCursor; | |
import android.database.ContentObserver; | |
import android.database.Cursor; | |
import android.database.DataSetObserver; | |
public class SplitCursor extends AbstractCursor { | |
private DataSetObserver default_observer = new DataSetObserver() { | |
@Override | |
public void onChanged() { | |
// Reset our position so the optimizations in move-related code | |
// don't screw us over | |
mPos = -1; | |
} | |
@Override | |
public void onInvalidated() { | |
mPos = -1; | |
} | |
}; | |
private Cursor cursor; | |
private int divisor; | |
private String[] new_columns; | |
public SplitCursor(Cursor cursor, int divisor) { | |
this.cursor = cursor; | |
this.divisor = divisor; | |
new_columns = new String[cursor.getColumnCount() * divisor]; | |
String[] names = cursor.getColumnNames(); | |
for (int i = 0; i < divisor; i++) { | |
for (int j = 0; j < cursor.getColumnCount(); j++) { | |
if (names[j].toLowerCase().equals("_id")) { | |
new_columns[i * cursor.getColumnCount() + j] = names[j]; | |
} else { | |
new_columns[i * cursor.getColumnCount() + j] = names[j] + "_" + (i+1); | |
} | |
} | |
} | |
cursor.registerDataSetObserver(default_observer); | |
} | |
@Override | |
public String[] getColumnNames() { | |
return new_columns; | |
} | |
@Override | |
public void close() { | |
cursor.close(); | |
super.close(); | |
} | |
@Override | |
public void deactivate() { | |
cursor.deactivate(); | |
super.deactivate(); | |
} | |
@Override | |
public byte[] getBlob(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getBlob(offset); | |
} | |
@Override | |
public int getCount() { | |
return cursor.getCount() / divisor + (cursor.getCount() % divisor != 0 ? 1 : 0); | |
} | |
@Override | |
public boolean onMove(int old_position, int new_position) { | |
return cursor.moveToPosition(new_position * divisor); | |
} | |
@Override | |
public double getDouble(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getDouble(offset); | |
} | |
@Override | |
public float getFloat(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getFloat(offset); | |
} | |
@Override | |
public int getInt(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getInt(offset); | |
} | |
@Override | |
public long getLong(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getLong(offset); | |
} | |
@Override | |
public short getShort(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getShort(offset); | |
} | |
@Override | |
public String getString(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getString(offset); | |
} | |
@Override | |
public boolean isNull(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.isNull(offset); | |
} | |
@Override | |
public int getType(int column) { | |
int index = column / cursor.getColumnCount(); | |
int offset = column % cursor.getColumnCount(); | |
cursor.moveToPosition(getPosition() * divisor + index); | |
return cursor.getType(offset); | |
} | |
@Override | |
public boolean isClosed() { | |
return cursor.isClosed(); | |
} | |
@Override | |
public boolean requery() { | |
return cursor.requery(); | |
} | |
@Override | |
public void registerContentObserver(ContentObserver observer) { | |
cursor.registerContentObserver(observer); | |
} | |
@Override | |
public void unregisterContentObserver(ContentObserver observer) { | |
cursor.unregisterContentObserver(observer); | |
} | |
@Override | |
public void registerDataSetObserver(DataSetObserver observer) { | |
cursor.registerDataSetObserver(observer); | |
} | |
@Override | |
public void unregisterDataSetObserver(DataSetObserver observer) { | |
cursor.unregisterDataSetObserver(observer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment