Filtering Realm object in android

So, I started using Realm, and everything works fine, and almost everything.

I use MultiAutoCompleteTextView to select some user (RealmObject)

so here:

this is my filter (inner class of my adapter)

private class UserFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            filterResults.values = mUsers;
            filterResults.count = mUsers.size();
        } else {
            final String lastToken = constraint.toString().toLowerCase();
            final List<User> list = new ArrayList<>();

            RealmQuery<User> query = realm.where(User.class);
            query.contains("nickname", lastToken, false);
            RealmResults<User> result = query.findAll();
            list.addAll(result);


            filterResults.values = list;
            filterResults.count = list.size();
        }
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilteredUsers = (List<User>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }

And in my adapter: in the getView method:

EGCUser user = getItem(position);

holder.mName.setText(user.getNickname());

the user is an invalid object, I tried so many different things and it didn’t work out. Therefore, I wonder what I can do with what I have achieved. I have a lot of problems with the stream, so maybe this is a problem with the inner class?

thank

EDIT: in this situation, where should I do Realm.getInstance ()?

Right now, I am passing the context in my adapter, and I am doing this in the adapter constructor, and I am storing the area object in a variable.

EDIT2: I got it to work, but I don’t know if this is how we should do it:

Filtering :

((Activity)mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    final List<User> list = new ArrayList<>();
                    Realm realm = Realm.getInstance(mContext);
                    RealmQuery<User> query = realm.where(User.class);
                    query.contains("nickname", lastToken, false);
                    RealmResults<User> result1 = query.findAll();
                    list.addAll(result1);

                    filterResults.values = list;
                    filterResults.count = list.size();

                }
            });

.

+4
2

. , Realm Filter - Thread ( Filter ). TODO, : https://github.com/realm/realm-android-adapters/issues/79

:

1) . Realm , , , , . , Realm, .

2) , Realm, , , performFiltering().

+3

. , , AutocompleteTextView. -, , . , , .

"wrapper":

public class RealmWrapper{

  private long oid;
  private String representation;

  public RealmWrapper(long oid, String representation) {
    this.oid = oid;
    this.representation = representation;
  }

  public long getOid() {
    return this.oid;
  }

  public void setOid(long oid) {
    this.oid = oid;
  }

  public String getRepresentation() {
    return representation;
  }

  public void setRepresentation(String representation) {
    this.representation = representation;
  }

  @Override
  public String toString() {
    return representation;
  }
}

Realm, :

 List<RealmWrapper> newObjects = new ArrayList<>();
 for( User entity : (RealmResults<User>) queryResults ){
     newObjects.add( new RealmWrapper(entity.getOid(), entity.getName()) );
 }

, RealmViewAdapter , :

User user = Realm.getInstance(context)
          .where(User.class)
          .contains("oid",adapter.getOid)
          .firstResult(); 

, !

+1

All Articles