如何在Firebase-Recycler视图中使用搜索活动

时间:2018-10-21 16:48:38

标签: android-studio firebase-realtime-database firebaseui

firebase DB

我已经设法对搜索活动进行编码,但是我没有得到图片,我搜索到的一些图片显示了一些

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search__wallpaper);

    recyclerView = findViewById(R.id.recycler_search_wallpaper);
    recyclerView.setHasFixedSize(true);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(gridLayoutManager);
    if(getIntent()!=null){
        search_query = getIntent().getStringExtra("search");
    loadBackgroundList(search_query);}
}

private void loadBackgroundList(String search_query) {
    query = FirebaseDatabase.getInstance().getReference(Common.STR_WALLPAPER)
            .orderByChild("imageName").startAt(search_query).endAt(search_query +"\uf8ff");
    options = new FirebaseRecyclerOptions.Builder<WallpaperItem>()
            .setQuery(query, WallpaperItem.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<WallpaperItem, ListWallpaperViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final ListWallpaperViewHolder holder, int position, @NonNull final WallpaperItem model) {

            link=model.getImageLink();
            GlideApp.with(getBaseContext())
                    .asBitmap()
                    .fitCenter()
                    .override(800,800)
                    .load(model.getImageLink())
                    .placeholder(R.drawable.ic_hourglass_custom_black_24dp)
                    .error(R.drawable.ic_error_outline_black_24dp)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.wallpaper);

            holder.setItemClickListener((view, position1) -> {
                Intent intent = new Intent(Search_Wallpaper.this, ViewWallpaper.class);
                Common.select_background = model;
                Common.select_background_key = adapter.getRef(position1).getKey();
                startActivity(intent);
            });
        }

        @NonNull
        @Override
        public ListWallpaperViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_wallpaper_item, parent, false);
            int hieght = parent.getMeasuredHeight() / 2;
            itemView.setMinimumHeight(hieght);
            return new ListWallpaperViewHolder(itemView);
        }
    };

    adapter.startListening();
    recyclerView.setAdapter(adapter);

}

@Override
protected void onStart() {
    super.onStart();
    if (adapter != null)
        adapter.startListening();
}

@Override
protected void onResume() {
    super.onResume();
    if (adapter != null)
        adapter.startListening();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.home, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    if (searchManager != null) {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
    }
    searchView.setMaxWidth(Integer.MAX_VALUE);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            loadBackgroundList(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            loadBackgroundList(query);
            return false;
        }
    });
    return true;    }

@Override
protected void onStop() {
    if (adapter != null)
        adapter.stopListening();
    super.onStop();
}

如果图像名称和图像描述包含搜索查询,则图像应显示在recyclerview中。

我已经返回了显示图像名称的搜索栏代码,但我只得到了其中的几个,我希望这个搜索名称应该是搜索firebase数据库,然后显示图像。

首先应检查名称,然后再进行描述

0 个答案:

没有答案