在搜索小部件中输入结果后如何获得结果

时间:2018-11-02 11:19:22

标签: android firebase android-studio

我正在使用Firebase,已附加我的代码。每次我通过应用程序的工具栏搜索小部件在Firebase数据库中按标题搜索特定标题时,搜索结果为空。我不知道有什么我想念的。我的搜索方法的代码在这里:

// search data
private void firebaseSearch(String searchText) {
    Query firebaseSearchQuery = mRef.orderByChild("title").startAt(searchText)
            .endAt(searchText + "\uf0ff)");

    FirebaseRecyclerOptions apartmentListOptions = new FirebaseRecyclerOptions.Builder<ApartmentList>().setQuery(firebaseSearchQuery, ApartmentList.class).build();

    mApartmentListAdapter = new FirebaseRecyclerAdapter<ApartmentList, ApartmentListViewHolder>(apartmentListOptions) {
        @Override
        public ApartmentListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.item_apartment,viewGroup, false);

            return new ApartmentListViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ApartmentListViewHolder holder, int position, @NonNull ApartmentList model) {
            holder.setItemThumbnail(getBaseContext(), model.getImage());
            holder.setImage2(model.getImage2());
            holder.setImage3(model.getImage3());
            holder.setTitle(model.getTitle());
            holder.setBathroomNumber(model.getBathroomNumber());
            holder.setBedroomNumber(model.getBedroomNumber());
            holder.setGarageNumber(model.getGarageNumber());
            holder.setCurrentDate(model.getCurrentDate());
            holder.setPricing(model.getPricing());
            holder.setLocation(model.getLocation());
            holder.setDesc(model.getDesc());
            holder.setItemThumbnail(getApplicationContext(), model.getImage());
        }
    };

    mApartmentList.setAdapter(mApartmentListAdapter);
    mApartmentListAdapter.notifyDataSetChanged();
}

及以下是我对工具栏的替代方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.item_toolbar_menu, menu);
    MenuItem item = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            firebaseSearch(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //filter as you type
            firebaseSearch(newText);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

谢谢

0 个答案:

没有答案