我正在尝试使用firebase实时数据库中的编辑文本进行搜索

1cklez4t  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(350)

它应该显示的数据和过滤器时,我搜索,但它没有显示任何数据。我以为我写了一些相当不错的代码,但我找不到我的错误。
我有一个模型班,有职位、公司、经验、年龄、薪水和类型。我制作了一个单独的布局xml文件。以下是firebase实时数据库结构。这是模型课。
下面是java代码

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_showjobs);
            ref = FirebaseDatabase.getInstance().getReference("jobs");

            searchbtn = findViewById(R.id.searchjobbtn);
            searchtxt = findViewById(R.id.searchjobtxt);
            result = (RecyclerView) findViewById(R.id.results);
            result.setHasFixedSize(true);
            result.setLayoutManager(new LinearLayoutManager(this));

            searchbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String searchedText = searchtxt.getText().toString();
                    firebasesearch(searchedText);
                }
            });
        }

        private void firebasesearch(String searchtxt) {
            Query firebaseSearchQuery=ref.orderByChild("jobTitle").startAt(searchtxt).endAt(searchtxt+"\uf8ff");

            FirebaseRecyclerOptions<jobclass> options = new FirebaseRecyclerOptions.Builder<jobclass>().setQuery(firebaseSearchQuery, jobclass.class).build();

            FirebaseRecyclerAdapter<jobclass, jobviewholder> recycleradaptor = new FirebaseRecyclerAdapter<jobclass, jobviewholder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull jobviewholder holder, int position, @NonNull jobclass model) {
                    holder.setDetails(model.getJobTitle(), model.getCompany(), model.getPay(), model.getType(), model.getExperience(), model.getAge());
                }

                @NonNull
                @Override
                public jobviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listlayout, parent, false);
                    jobviewholder jv = new jobviewholder(v);
                    return jv;
                }
            };
            result.setAdapter(recycleradaptor);
            Toast.makeText(showjobs.this, "Searching", Toast.LENGTH_LONG).show();

        }
      /*  @Override
        protected void onStart() {
            super.onStart();
            FirebaseRecyclerOptions<jobclass> options = new FirebaseRecyclerOptions.Builder<jobclass>().setQuery(ref, jobclass.class).build();

            Toast.makeText(showjobs.this, "Searching", Toast.LENGTH_LONG).show();
            FirebaseRecyclerAdapter<jobclass, jobviewholder> recycleradaptor = new FirebaseRecyclerAdapter<jobclass, jobviewholder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull jobviewholder holder, int position, @NonNull jobclass model) {
                    holder.setDetails(model.getJobTitle(), model.getCompany(), model.getPay(), model.getType(), model.getExperience(), model.getAge());
                }

                @NonNull
                @Override
                public jobviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listlayout, parent, false);
                    jobviewholder jv = new jobviewholder(v);
                    return jv;
                }
            };
            result.setAdapter(recycleradaptor);
        }*/

        public class jobviewholder extends RecyclerView.ViewHolder {

            View v;

            public jobviewholder(View itemView){
                super(itemView);
                v=itemView;
            }
            public void setDetails(String jobn, String companyn, String pay, String Ag, String xreq, String jtyp){
                TextView jobtitle=findViewById(R.id.jname);
                TextView companyname=findViewById(R.id.cname);
                TextView payment=findViewById(R.id.sal);
                TextView age=findViewById(R.id.agereq);
                TextView exprequired=findViewById(R.id.reqex);
                TextView jobtype=findViewById(R.id.type);

                jobtitle.setText(jobn);
                companyname.setText(companyn);
                payment.setText(pay);
                age.setText(Ag);
                exprequired.setText(xreq);
                jobtype.setText(jtyp);
            }
        }
    }

谢谢你抽出时间。我希望有人能帮我找到问题。

vsikbqxv

vsikbqxv1#

result.setAdapter(recycleradaptor);
            Toast.makeText(showjobs.this, "Searching", Toast.LENGTH_LONG).show();

为了这个 recycleradaptor 你也得开始听。你得在家里听 onStart() 所以你可以

@Override
protected void onStart() {
    super.onStart();
    recycleradaptor.startListening();
}
jutyujz0

jutyujz02#

这是使用firebase数据库进行搜索的工作代码`

private void  searchFriend(String text_search) {
    Query query = FirebaseDatabase.getInstance().getReference(Common.USERS)
            .orderByChild("name")
            .startAt(text_search)
            .endAt(text_search + "\uf8ff");

    FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
            .setQuery(query, User.class)
            .setLifecycleOwner(this)
            .build();

    searchAdapterQuery = new FirebaseRecyclerAdapter<User, UserViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final UserViewHolder holder, int position, @NonNull final User user) {
            holder.userName.setText(user.getName());

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }

        @NonNull
        @Override
        public UserViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View itemView = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.all_user_recycler_view_item, viewGroup, false);

            return new UserViewHolder(itemView);
        }
    };

    searchAdapterQuery.startListening();
    recyclerView.setAdapter(searchAdapterQuery);
}`

相关问题