Created
November 18, 2017 00:26
-
-
Save randypfohl/cf409a2a3de93224b462cd5696b79459 to your computer and use it in GitHub Desktop.
Built with the idea of large data and not wanting to re-instantiate data after pulling form a database. Easily changed to support more efficient filtering based on previous results
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
import android.database.Cursor; | |
import android.database.CursorWrapper; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
// todo optimize this to not filter through entire array for continuous entries or deletions | |
public class FilterableGuestCursor extends CursorWrapper { | |
public static String TAG = FilterableGuestCursor.class.getSimpleName(); | |
FilterListener mFilterListener; | |
private String filter; | |
private int[] index; | |
private int filterCount; | |
private int count = 0; | |
private int pos = 0; | |
public FilterableGuestCursor(Cursor cursor, String filter, FilterListener mFilterListener) { | |
super(cursor); | |
this.mFilterListener = mFilterListener; | |
this.filter = filter.toLowerCase(); | |
this.filterCount = this.count = super.getCount(); | |
this.index = new int[this.count]; | |
for (int i = 0; i < this.count; i++) { | |
this.index[i] = i; | |
} | |
} | |
public void refreshFilter(String filter) { | |
this.filter = filter.toLowerCase(); | |
new FilterTask().execute(); | |
} | |
private void populateFilterIndex() { | |
if (!this.filter.equals("")) { | |
this.count = super.getCount(); | |
this.index = new int[this.count]; | |
this.pos = 0; | |
for (int i = 0; i < this.count; i++) { | |
Log.d(TAG, "Cursor Index:" + i + ", Array Index :" + pos); | |
super.moveToPosition(i); | |
String fullName = this.getString(#insert cursor column matching here); | |
if (fullName.toLowerCase().contains(filter)) | |
this.index[this.pos++] = i; | |
} | |
this.filterCount = this.pos; | |
super.moveToFirst(); | |
} | |
else { | |
this.filterCount = this.count = super.getCount(); | |
this.index = new int[this.count]; | |
for (int i = 0; i < this.count; i++) { | |
this.index[i] = i; | |
} | |
} | |
} | |
@Override | |
public boolean move(int offset) { | |
return this.moveToPosition(this.pos + offset); | |
} | |
@Override | |
public boolean moveToNext() { | |
return this.moveToPosition(this.pos + 1); | |
} | |
@Override | |
public boolean moveToPrevious() { | |
return this.moveToPosition(this.pos - 1); | |
} | |
@Override | |
public boolean moveToFirst() { | |
return this.moveToPosition(0); | |
} | |
@Override | |
public boolean moveToLast() { | |
return this.moveToPosition(this.filterCount - 1); | |
} | |
@Override | |
public boolean moveToPosition(int position) { | |
if (position >= this.filterCount || position < 0) | |
return false; | |
this.pos = position; | |
return super.moveToPosition(this.index[position]); | |
} | |
@Override | |
public int getCount() { | |
return this.filterCount; | |
} | |
@Override | |
public int getPosition() { | |
return this.pos; | |
} | |
public interface FilterListener{ | |
public void onFiltered(); | |
} | |
public class FilterTask extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... aVoid) { | |
populateFilterIndex(); | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) { | |
mFilterListener.onFiltered(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment