Couchbase灯光过滤数据,在Android

时间:2017-06-17 11:50:29

标签: android couchbase couchbase-lite

我正在使用Couchbase作为我的应用程序,我面临着使用recyclerview的搜索数据问题,我不能使用过滤器逻辑,因为我没有列表,我使用livequery来填充适配器,请看下面的代码。

private LiveQuery mQuery;
    private QueryEnumerator enumerator;

    public AllRecipeAdapter(Activity activity, ClickListner clickListner, LiveQuery query) {
        this.mActivity = activity;
        this.mClickListner = clickListner;
        this.mQuery = query;
        this.aListDocIds = new ArrayList<>();
        refeshData();
    }

    private void refeshData() {
        mQuery.addChangeListener(new LiveQuery.ChangeListener() {
            @Override
            public void changed(final LiveQuery.ChangeEvent event) {
                ((Activity) mActivity).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        enumerator = event.getRows();
                        notifyDataSetChanged();
                    }
                });
            }
        });
        mQuery.start();
        notifyDataSetChanged();
        Log.e("", "refeshData: ");
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recipes, parent, false);
        return new ViewHolder(mView);
    }

    @Override
    public int getItemCount() {
        return enumerator != null ? enumerator.getCount() : 0;
    }

    public Document getItem(int position) {
        return enumerator != null ? enumerator.getRow(position).getDocument() : null;
    }

    public void invalidate() {
        if (mQuery != null)
            mQuery.stop();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ViewHolder) {
            Document document = (Document) getItem(position);
//            Log.e(TAG, "onBindViewHolder: " + document.getId());
            ImageView imgFeaturedRecipe = ((ViewHolder) holder).imgFeaturedRecipe;
            String strDocId = (String) document.getProperty(_ID);
            aListDocIds.add(strDocId);
            String strRecipeName = (String) document.getProperty(RECIPENAME);
            String strCookingTime = (String) document.getProperty(RECIPECOOKINGTIME);
            String imgId = (String) document.getProperty(IMAGEID);
            ((ViewHolder) holder).txtRecipeName.setText(strRecipeName);
            ((ViewHolder) holder).txtRecipeTime.setText(strCookingTime);
            ImageLoader.getInstance().setDisplayImage(mActivity, mActivity.getString(R.string.imageUrl) + imgId, imgFeaturedRecipe);
        }
    }

//在适配器构造函数中获取查询传递。

private Query getQuery() {
        return CBLSyncManager.getInstance(this).getListsView().createQuery();
    }

/**
     * Database View
     */
    public View getListsView() {
        View view = mDatabase.getView(VIEW_RECIPELIST);
        if (view.getMap() == null) {
            Mapper mapper = new Mapper() {
                public void map(Map<String, Object> document, Emitter emitter) {
                    if (document.get(_ID).toString().contains(RCP)) {
                        String docID = (String) document.get(ID);
                        String strRecipeName = (String) document.get(RECIPENAME);
                        String strCookingTime = (String) document.get(RECIPECOOKINGTIME);
                        String[] temp = new String[]{docID, strRecipeName, strCookingTime};
                        emitter.emit(temp, null);

                    }

                }
            };
            view.setMap(mapper, "1.0");
        }
        return view;
    }

我想在搜索框中将数据过滤为用户类型。

1 个答案:

答案 0 :(得分:1)

每次仅实时查询刷新

  1. 数据库更改时(如添加/修改/删除任何文档)。
  2. 更改查询版本号。
  3. 为了启用搜索功能, 立即从所有文档中发出所需的属性,并将其保存在地图中并进行搜索。