It's time to look for another solution, but I just want to add this, because I think the answers are unsatisfactory.
You can easily create your own cursor class. To allow functions requiring the cursor to accept it, it must extend AbstractCursor. To overcome the problem with a system not using your class, you simply make your class a wrapper.
Here is a really good example. https://android.googlesource.com/platform/packages/apps/Contacts/+/8df53636fe956713cc3c13d9051aeb1982074286/src/com/android/contacts/calllog/ExtendedCursor.java
public class ExtendedCursor extends AbstractCursor { private final Cursor mCursor; private final String mColumnName; private final Object mValue; public ExtendedCursor(Cursor cursor, String columnName, Object value) { mCursor = cursor; mColumnName = columnName; mValue = value; } @Override public int getCount() { return mCursor.getCount(); } @Override public String[] getColumnNames() { String[] columnNames = mCursor.getColumnNames(); int length = columnNames.length; String[] extendedColumnNames = new String[length + 1]; System.arraycopy(columnNames, 0, extendedColumnNames, 0, length); extendedColumnNames[length] = mColumnName; return extendedColumnNames; }
This is a general idea of ββhow this will work.
Now to the meat problems. To prevent performance gains, create a hash to hold the column indexes. This will serve as a cache. When getString is called, check the hash for the column index. If it does not exist, then extract it using getColumnIndex and cache it.
Sorry, I cannot add the code at present, but I am on a mobile device, so I will try to add it later.
source share