如何运行在运行与Firebase连接的方法时遇到问题的代码?

时间:2019-01-07 17:38:24

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

我想让人们在单击Enter键时搜索单词。这些词在Firebase数据库中。此片段代码用于在编写后列出帖子。我想添加搜索功能,但是某些方法无法再次使用。它们仅工作一次(列出帖子时)。我整天都在挣扎!

我的代码不适用于private void firebaseUserSearch(String searchText)的日志号19,14,但是onCreateViewHolderonBindViewHolder首先起作用(private void fetch()的日志号19,1444)。 / p>

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_two, container, false);
        rv = view.findViewById(R.id.recycleViewContainer);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(linearLayoutManager);
        //rv.setHasFixedSize(true);
        fetch();

        write_btn = view.findViewById(R.id.btn_write);
        write_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Post_Activity.class);
                startActivity(intent);
            }
        });


        mUserDatabase = FirebaseDatabase.getInstance().getReference("posts");
        mSearch = (android.support.v7.widget.SearchView) view.findViewById(R.id.mSearch);


        mSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.d("aaa", "1"+query);
                String searchText = query;
                Log.d("aaa", "2"+searchText);

                firebaseUserSearch(searchText);
                Log.d("aaa", "3");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
            return view;
    }


    private void fetch() {

        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("posts");
        Log.d("aaa", "10" + query);
        FirebaseRecyclerOptions<Post> options =
                new FirebaseRecyclerOptions.Builder<Post>()
                        .setQuery(query, new SnapshotParser<Post>() {
                            @NonNull
                            @Override
                            public Post parseSnapshot(@NonNull DataSnapshot snapshot) {
                            return new 
Post(snapshot.child("id").getValue().toString(),

snapshot.child("title").getValue().toString(),

snapshot.child("content").getValue().toString());
                            }
                        })
                        .build();
        Log.d("aaa", "11" + options);

        adapter = new FirebaseRecyclerAdapter<Post, ViewHolder>(options) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);
                Log.d("aaa", "19");


                return new ViewHolder(view);

            }


            @Override
            protected void onBindViewHolder(ViewHolder holder, final int 
position, Post post) {
                holder.setTxtTitle(post.getTitle());
                holder.setTxtContent(post.getContent());
                Log.d("aaa", "1444" + holder);
                holder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getActivity(), 
String.valueOf(position), Toast.LENGTH_SHORT).show();
                    }
                });
            }

        };


        rv.setAdapter(adapter);

        Log.d("aaa", "199");




    }

    private void firebaseUserSearch(String searchText) {

        Query firebaseSearchQuery = 
mUserDatabase.orderByChild("title").startAt(searchText).endAt(searchText + 
"\uf8ff");
        Log.d("aaa", "3"+mUserDatabase);
        Log.d("aaa", "3"+firebaseSearchQuery);
        FirebaseRecyclerOptions<Post> options2 =
                new FirebaseRecyclerOptions.Builder<Post>()
                        .setQuery(firebaseSearchQuery, new 
SnapshotParser<Post>() {
                            @NonNull
                            @Override
                            public Post parseSnapshot(@NonNull DataSnapshot 
 snapshot) {
                                return new 
Post(snapshot.child("id").getValue().toString(),

snapshot.child("title").getValue().toString(),

snapshot.child("content").getValue().toString());
                            }
                        })
                        .build();

        Log.d("aaa", "3"+options2);


        Log.d("aaa", "333");

        adapter = new FirebaseRecyclerAdapter<Post, ViewHolder>(options2) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);
                Log.d("aaa", "19");


                return new ViewHolder(view);

            }


            @Override
            protected void onBindViewHolder(ViewHolder holder, final int 
position, Post post) {
                holder.setTxtTitle(post.getTitle());
                holder.setTxtContent(post.getContent());
                Log.d("aaa", "14" + holder);
                holder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getActivity(), 
String.valueOf(position), Toast.LENGTH_SHORT).show();
                    }
                });
            }

        };
        rv.setAdapter(adapter);
    }

我不知道为什么这些方法没有再次激活。我的目标是显示用户找到的帖子。谢谢您的阅读!

1 个答案:

答案 0 :(得分:0)

问题解决了。 所以感到忧郁 “ mUserDatabase = FirebaseDatabase.getInstance()。getReference(” posts“);” 我改变了这部分。还有很多?东西。 我确实挖了很多东西。结论是主要问题是我的查询错误。 如果查询错误,则与firebaserecycleradapter链接的方法无效。 谢谢再见