2017-06-17 63 views
0

我正在爲我的應用程序使用Couchbase,我面臨使用recyclerview搜索數據的問題,我無法使用過濾器邏輯,因爲我沒有列表,我使用livequery來填充適配器,請看下面的代碼。Couchbase light過濾器數據,在Android中執行搜索

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

實況查詢刷新每次只

  1. 當數據庫的變化(比如,添加/修改/刪除任何文件)。
  2. 更改查詢版本號。

對於啓用搜索功能, 一次從所有文檔中發出所需的屬性並將其保存在地圖中並進行搜索。