gpt4 book ai didi

java - Firebase 搜索功能

转载 作者:行者123 更新时间:2023-12-02 01:44:15 31 4
gpt4 key购买 nike

代码

        mRecyclerView = (RecyclerView) rootview.findViewById(R.id.recyclerView);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);


mDataabseReference = FirebaseDatabase.getInstance().getReference().child("UserData");


mSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String mSearchText = mEditText.getText().toString();

mSearch(mSearchText);
}
});
return rootview;
}

private void mSearch(final String mSearchText) {
Query searchQuery = mDataabseReference;

FirebaseRecyclerOptions<SearchHelper> options =
new FirebaseRecyclerOptions.Builder<SearchHelper>()
.setQuery(searchQuery, SearchHelper.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<SearchHelper, Search.SearchViewHolder>(options) {

@Override
protected void onBindViewHolder(@NonNull final Search.SearchViewHolder holder, int position, @NonNull final SearchHelper model) {


String uid = getRef(position).getKey();
mDataabseReference.child(uid).startAt(mSearchText).endAt(mSearchText + "\uf8ff").addValueEventListener(new ValueEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

for (DataSnapshot ds : dataSnapshot.getChildren()) {
SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
mSearchList.add(search);

}
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
mSearchList.add(search);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}


};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();

}

public static class SearchViewHolder extends RecyclerView.ViewHolder {
View mView;

public SearchViewHolder(View itemView) {
super(itemView);
mView = itemView;
}

public void setName(String title) {
TextView nameText = (TextView) mView.findViewById(R.id.name);
nameText.setText(title);
}
public void setUserName(String title) {
TextView nameText = (TextView) mView.findViewById(R.id.username);
nameText.setText(title);
}
public void setImage(String image) {
CircularImageView mDisplayImage = (CircularImageView) mView.findViewById(R.id.circleImageView);
Picasso.get().load(image).into(mDisplayImage);
}

没有显示结果...

我尝试过Childevent和ValueEvent我尝试过使用 for 循环和不使用它

数据库

edon't image description here

我不知道出了什么问题,我厌倦了尝试,因为也没有错误,而且我不知道哪里出了问题。有人可以帮我指出错误吗?

最佳答案

在 Firebase 中搜索始终是一个两步方法:

  1. 对数据进行排序
  2. 根据该顺序过滤数据

您的代码中缺少第 1 步:您没有指明要订购什么。

例如,如果您尝试按名称搜索用户日期,您的查询将变为:

mDataabseReference.child(uid)
.orderByChild("Name")
.startAt(mSearchText).endAt(mSearchText + "\uf8ff")
.addValueEventListener(new ValueEventListener() {

另一个错误是您正在 onChildAdded 中查找 dataSnapshot.getChildren()。但是 onChildAdded 已经为每个单独的子节点触发,因此不需要循环:

 public void onChildAdded(DataSnapshot dataSnapshot, String s) {
SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
mSearchList.add(search);
}

关于java - Firebase 搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940961/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com