FirebaseRecyclerAdapter搜索

时间:2018-04-10 16:58:03

标签: java android firebase firebase-realtime-database

我已经实施了FirebaseRecyclerAdapter,可以将数据库中的城市填充到RecyclerView中。在几个未能实现搜索功能后,我正在寻求帮助。我想让用户通过输入城市名称(postName)来搜索特定城市。我们的想法是在搜索字段中键入其名称后,在开头和所需城市填充所有可用城市。

Firebase database

App image (Big image)

我填充视图的代码是:

   searchField = view.findViewById(R.id.search_field);
    searchButton = view.findViewById(R.id.imageButton);

    searchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String search_field = searchField.getText().toString().trim();
            initialiseScreen(view, search_field);
            Toast.makeText(getContext(), search_field, Toast.LENGTH_SHORT).show();
        }
    });

    String search_field = null;
    initialiseScreen(view, search_field);


    return  view;

}

 private void initialiseScreen(final View view, String searchText) {


        Query postQuery = mDataRef.orderByChild("postName").startAt(searchText).endAt(searchText + "\uf8ff");

        mDataRef.keepSynced(true);

        recyclerView = view.findViewById(R.id.post_RV);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mPostViewAdapter);

        FirebaseRecyclerOptions postOptions = new FirebaseRecyclerOptions.Builder<Post>()
                .setQuery(postQuery, Post.class).build();

        mPostViewAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(postOptions) {
            @Override
            protected void onBindViewHolder(PostViewHolder holder, int position, final Post model) {

               final String post_key = getRef(position).getKey();
               holder.setPostCityImage(model.getImageURL());
               holder.setPostCityName(model.getPostName());
               holder.setLikeBtn(post_key);
               //When is clicked once go to city fragment
               holder.cityImg.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       //Add code here

                       Intent singlePostIntent = new Intent(getActivity(), CitiesActivity.class);
                       singlePostIntent.putExtra("blog_id", post_key);
                       startActivity(singlePostIntent);

                   }
               });

               //Likes button
              holder.likes.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mProccessLike = true;

                      String postId = model.getmUid();


                          mDatabaseLikesRef.addValueEventListener(new ValueEventListener() {
                              @Override
                              public void onDataChange(DataSnapshot dataSnapshot) {

                                  if(mProccessLike) {

                                      if (dataSnapshot.child(post_key).hasChild(current_user_id)) {


                                          mDatabaseLikesRef.child(post_key).child(current_user_id).removeValue();
                                          mProccessLike = false;

                                      } else {

                                          mDatabaseLikesRef.child(post_key).child(current_user_id).setValue("RandomValue");
                                          mProccessLike = false;

                                      }

                                  }

                              }

                              @Override
                              public void onCancelled(DatabaseError databaseError) {

                              }
                          });
                  }
              });
//

            }

如果不是字符串search_field = null; ,它只会填充一个城市字符串search_field =&#34; Calp,西班牙&#34 ;; < / p>

0 个答案:

没有答案