簡體   English   中英

如何使用Firebase Firestore進行用戶名搜索?

[英]How to make a username search using Firebase Firestore?

我目前正在嘗試通過Firestore查詢實現搜索方法,以便用戶可以通過在EditText中鍵入其用戶名來搜索其他用戶,該用戶名應在RecyclerView中顯示結果。 我嘗試了其他一些代碼變體,但似乎查詢從未觸發,並且我不知道問題出在哪里。 任何幫助深表感謝!

這是我在搜索字段中的活動:

public class PlayerSearch extends AppCompatActivity {

    private EditText searchField;
    private View okBtn;

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference userRef = db.collection("User");

    private UserAdapter userAdapter;

    // TODO: 2019-11-11 Make a method that takes the input of the EditText and make a query in Firestore to search a player and display the result

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

        searchField = findViewById(R.id.edit_text_search);
        okBtn = findViewById(R.id.search_player_ok);

        RecyclerView recyclerView = findViewById(R.id.recycler_view_search_player);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        String searchText = searchField.getText().toString();

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

                search(searchText);

            }
        });
    }

    private void search(String searchText) {
        Query query = userRef.orderBy("username").startAt(searchText).endAt(searchText + "\uf8ff");

        FirestoreRecyclerOptions<Users> options = new FirestoreRecyclerOptions.Builder<Users>()
                .setQuery(query, Users.class)
                .build();

        userAdapter = new UserAdapter(options);

    }

這是我的UserAdapter:

public class UserAdapter extends FirestoreRecyclerAdapter<Users, UserAdapter.UserHolder> {

    public UserAdapter(@NonNull FirestoreRecyclerOptions<Users> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull UserHolder holder, int position, @NonNull Users model) {

        holder.textViewUsername.setText(model.getUsername());

    }

    @NonNull
    @Override
    public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_result,parent, false);
        return new UserHolder(view);
    }

    class UserHolder extends RecyclerView.ViewHolder {

        TextView textViewUsername;

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

            textViewUsername = itemView.findViewById(R.id.player_name_search);
        }
    }
}

這是我的User類:


public class Users {

    public Users (){

    }

    public String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Users(String username) {
        this.username = username;
    }
    }

您的search方法將創建一個新適配器,但不會以任何方式將其與recyclerView關聯。

因此,您將需要以下內容:

private void search(String searchText) {
    Query query = userRef.orderBy("username").startAt(searchText).endAt(searchText + "\uf8ff");

    FirestoreRecyclerOptions<Users> options = new FirestoreRecyclerOptions.Builder<Users>()
            .setQuery(query, Users.class)
            .build();

    userAdapter = new UserAdapter(options);

    RecyclerView recyclerView = findViewById(R.id.recycler_view_search_player)

    recyclerView.setAdapter(userAdapter);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM