New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filterable RealmBaseAdapter and AutocompleteTextView #1726
Comments
Realm searches are so fast, why do you even need to perform them in a background thread? :) |
@Saketme I'm not, as you can see on the example code, I ignore the performfiltering method (is the one made in background) and do the actual filtering on the publish filter method. The problem is that the list is not being filtered on the UI. |
Ah. So in the meanwhile, you should try doing it without a Filter class then. |
At this moment I'm just using a ArrayAdapter and handling manually the validation and texteditchanged but it is very patchy. |
@gpulido, do you have a snippet of your workaround to share? |
@freezy I had to create a "mini POCO class" with just the basic info I need ffrom the original RealmObject, and transforming the RealmResults list to a list of this POCO class before creating a basic arrayAdapter. So at the end I'm holding a "non realm supported" list. As I'm using this just for a spinner that is being populated almost every time, it is not a problem. It is not the best or prettier solution but it works for me until Realm comes with a cleaner solution. |
I've tried something like this and filtering seems to work so far: public class FilterableRealmAdapter<T extends RealmObject> extends ArrayAdapter<T> implements Filterable {
final RealmResults<T> mRealmObjectList;
List<T> mResults;
public FilterableRealmBaseAdapter(Context context, RealmResults<T> realmObjectList, MaterialAutoCompleteTextView refAutocomplete) {
super(context, android.R.layout.simple_list_item_1);
mRealmObjectList = realmObjectList;
refAutocomplete.setAdapter(this);
}
@Override
public int getCount() {
return mResults == null ? 0 : mResults.size();
}
@Override
public T getItem(int position) {
return mResults == null ? null : mResults.get(position);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return new FilterResults();
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (constraint != null) {
mResults = mRealmObjectList.where().contains("name", String.valueOf(constraint)).findAll();
notifyDataSetChanged();
}
}
};
}
} However, still need to figure out how to nicely populate the view, but this isn't Realm related. It would be interesting to see your solution as well. |
There's one gotcha here if you're using an adapter based EditText (AutocompleteTextView, etc.). In the (To be more precise, AutocompleteTextView uses the |
@freezy Your solution is very similar to my first approach (at the top of this issue), but you manage to solve the substitution of the filtered list (I don't know why I didn't figure out that way). |
Alright, thanks for commenting. No particular problem with the view, I just hadn't done it at the time I posted the comment. I'll paste the refactored class with @Saketme's input in #646. |
Hey,can you provide an example code on autocomplete textview ,where we put the filter data from realm objects,am a newbee,please do help |
Hello,
I'm trying to populate an AutocompleteTextView with a list of "RealmResults" and filter the list while the user enter some keys.
I'm extending the RealmBaseAdapter as follows:
AS the Filter class uses the perform filtering on a working thread different that the one is used to create the RealmResults, I just not use that and do the actual filtering process on the publishResults.
The problem is that the list is not being filtered. I have checked the realmResults after the "filter" phase on the publishResults method and it is filtered, but the list on the UI still contains all the items of the original realmResults.
What I am missing here?
Thank you
Gabriel
The text was updated successfully, but these errors were encountered: