如何运行有问题的代码来运行与Firebase连接的方法



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

我的代码在private void firebaseUserSearch(String searchText)的日志编号19,14处不工作,但onCreateViewHolderonBindViewHolder首先工作(private void fetch()的日志编号191444)。

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);
}

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

问题已解决。所以感到忧郁。"mUserDatabase=FirebaseDatabase.getInstance().getReference("posts");"我改了这个部分。还有很多?事情。我挖了很多。结论是,主要问题是我的错误查询。如果查询错误,则与firebaserecycleradapter链接的方法将不起作用。谢谢你,再见

相关内容

  • 没有找到相关文章

最新更新