gpt4 book ai didi

java - 按用户名在 FirebaseDatabase 中搜索用户

转载 作者:行者123 更新时间:2023-11-29 18:34:01 27 4
gpt4 key购买 nike

我正在尝试使用简单的搜索栏 (EditText) 按用户名搜索存储在我的 firebase 数据库中的用户,并在单击按钮时显示该特定用户

我的 findfriends.java 文件中有以下代码

public class FindFriendsActivity extends AppCompatActivity {

private EditText seachTextBox;
private ImageView searchImage;
private RecyclerView searchResultList;
private DatabaseReference allUsersDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friends);

searchResultList = findViewById(R.id.search_result_list);
searchResultList.setHasFixedSize(true);
searchResultList.setLayoutManager(new LinearLayoutManager(this));

allUsersDatabase = FirebaseDatabase.getInstance().getReference().child("users");

seachTextBox = findViewById(R.id.searchBox);
searchImage = findViewById(R.id.searchImage);

searchImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String seachbox = seachTextBox.getText().toString();

final Query searchpeople = allUsersDatabase.orderByChild("username").startAt(seachbox)
.endAt(seachbox + "\uf8ff");
}
});

}

@Override
protected void onStart()
{
super.onStart();

FirebaseRecyclerOptions<FindFriends> options =
new FirebaseRecyclerOptions.Builder<FindFriends>()
.setQuery(allUsersDatabase, FindFriends.class)
.build();

FirebaseRecyclerAdapter<FindFriends, FindFriendsHolder> adapter =
new FirebaseRecyclerAdapter<FindFriends, FindFriendsHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FindFriendsHolder holder, int position, @NonNull FindFriends model)
{
holder.username.setText(model.getUsername());
holder.fullname.setText(model.getFullname());
Picasso.get().load(model.getProfileimage()).placeholder(R.drawable.friends_result_icon).fit().into(holder.userimage);
}

@NonNull
@Override
public FindFriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_friends_result_layout,viewGroup,false);
FindFriendsHolder viewHolder = new FindFriendsHolder(view);
return viewHolder;
}
};

searchResultList.setAdapter(adapter);
adapter.startListening();
}


public static class FindFriendsHolder extends RecyclerView.ViewHolder
{

TextView username, fullname;
ImageView userimage;

public FindFriendsHolder(@NonNull View itemView)
{
super(itemView);

username = itemView.findViewById(R.id.username_result);
fullname = itemView.findViewById(R.id.fullname_result);
userimage = itemView.findViewById(R.id.friend_result_image);
}

}

}///end

我做了一些研究,发现我需要创建一个查询并将其设置为 Startat(Edittext).endAt(EditText) 但我不确定实际插入查询的位置。

使用以下代码,我能够显示数据库中的每个用户。

任何帮助都会很棒!

最佳答案

根据您的评论,您现在获得了所有用户。如果只想获取单个用户,请更改以下代码行:

Query searchpeople = allUsersDatabase.orderByChild("username").startAt(seachbox)
.endAt(seachbox + "\uf8ff");

Query searchpeople = allUsersDatabase.orderByChild("username").equalTo(seachbox);

关于java - 按用户名在 FirebaseDatabase 中搜索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55055319/

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