使用多个未知的标识符计数来查询RealmDB

时间:2016-10-20 17:34:23

标签: android realm

我有一个名为RealmItem的数据库,我让所有用户都附加了地区。哪个n size未知。每个项目都有一个名为territory的字段。现在,有没有办法查询Realm以查找具有附加到用户的任何Item Records的所有territoryID。我之所以需要这个,是因为我将AutoCompleteTextView传递给了一个如下所示的过滤适配器:

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();
        }
    }
}

更像是寻找类似这样的东西.equalTo("id", [array of Ids]) <-- This is where I need it或任何好的过滤使得过滤无法顺利

1 个答案:

答案 0 :(得分:1)

你基本上只是在寻找the in() operator, added in 1.1.0吗?

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