Ituoke Ajanlekoko Ituoke Ajanlekoko - 1 month ago 7
Android Question

Using Multiple Unknown Count of Identifiers to query RealmDB

I have a DB named

RealmItem
and I have all users have territories they are attached to. Which is
n size
unknown. Every item has a field called
territory
. Now, is there a way to query
Realm
to find all
Item Records
that has any of the
territoryID
attached to the user. The reason I'm in need of this is because, I pipe a
AutoCompleteTextView
to a filtering adapter that looks like this:

public class ItemFilterAdapter extends BaseAdapter implements Filterable {

private static final String TAG = ItemFilterAdapter.class.getSimpleName();
private Context mContext;
private List<RealmItem> mResult = new ArrayList<>();
private LayoutInflater inflater;
private boolean needsFilter = false;

public ItemFilterAdapter(Context mContext) {
this.mContext = mContext;
}

public void setNeedsFilter(boolean needsFilter) {
this.needsFilter = needsFilter;
}

@Override
public int getCount() {
return mResult.size();
}

@Override
public Object getItem(int position) {
return mResult.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (view == null)
view = inflater.inflate(R.layout.item_update_autocomplete, parent, false);

RealmItem item = mResult.get(position);

TextView updateName = (TextView) view.findViewById(R.id.update_name);
TextView updateAddress = (TextView) view.findViewById(R.id.update_address);

if (item != null) {
updateName.setText(item.getName());
try {
JSONObject obj = new JSONObject(item.getPayload());
String address = obj.getString("address");
updateAddress.setText(address);
} catch (Exception e) {
e.printStackTrace();
}
}

return view;
}

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
return null;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
if (constraint != null) {
mResult = filterDealers(constraint.toString());
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}

@NonNull
private List<RealmItem> filterDealers(String query) {
Realm mRealm = RealmUtils.getRealmInstance(mContext);

if (!needsFilter) {
return mRealm.where(RealmItem.class)
.contains("name", query, Case.INSENSITIVE)
.equalTo("id", [array of Ids]) <-- This is where I need it
.findAll();
} else {
return mRealm.where(RealmItem.class)
.contains("name", query, Case.INSENSITIVE)
.notEqualTo("activeStatus", "inactive")
.equalTo("id", [array of Ids]) <-- This is where I need it
.findAll();
}
}
}


More like looking for something like this
.equalTo("id", [array of Ids]) <-- This is where I need it
or anything nice to make filtering seemlessly smooth

Answer

Aren't you basically just looking for the in() operator, added in 1.1.0?

public RealmQuery<E> in(
      String fieldName, Integer[] values)
Comments