Last active
June 29, 2020 07:59
-
-
Save ryjen/e63502afe677f00007e9 to your computer and use it in GitHub Desktop.
Android: How to get the correct CursorAdapter position inside a MergeAdapter (cwac.commonsware.com)
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
/** | |
* gets [section,item idx in section] for raw list position | |
* | |
* @param adapter | |
* @param position: raw position in {@link com.commonsware.cwac.merge.MergeAdapter} | |
* @return ArrayList<Integer> | |
*/ | |
public static Pair<Integer, Integer> getMergeAdapterPos(MergeAdapter adapter, int | |
position) { | |
int section = -1; | |
int sectionStart = -1; | |
for (int i = 0; i <= position; i++) { | |
if (!(adapter.getItem(i) instanceof Cursor)) { | |
sectionStart = i; | |
section++; | |
} | |
} | |
int correctedPos = position - sectionStart - 1; | |
return new Pair<Integer, Integer>(section, correctedPos); | |
} | |
@Override | |
public void onListItemClick(ListView l, View v, int position, long id) { | |
Adapter adapter = mergeAdapter.getAdapter(position); | |
if (adapter instanceof CursorAdapter) { | |
CursorAdapter cursorAdapter = (CursorAdapter) adapter; | |
Pair<Integer, Integer> realPosition = getMergeAdapterPos(mergeAdapter, position); | |
cursorAdapter.getCursor().moveToPosition(realPosition.second); | |
String rowId = cursorAdapter.getCursor().getString(cursorAdapter.getCursor().getColumnIndex("_id")); | |
// do other stuff | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment