如何使用firebaseRecyclerAdapter

时间:2018-06-20 16:09:31

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

我正在尝试使用firebaseRecyclerAdapter为android studio中的用户实现搜索功能。我想要的是让用户在编辑文本字段中键入其他用户,并使用Firebase Recycler适配器执行搜索以显示搜索结果。新的Firebase用户界面要求Firebase适配器在on create方法中进行不同的定义。我不确定如何使用新的Firebase适配器实现搜索查询。

SearchUserActivity:

public class SearchUserActivity extends AppCompatActivity {


private RecyclerView mRecyclerView;
private EditText mSearchField;
private ImageButton mSearchButton;
private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
FirebaseRecyclerAdapter adapter;

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}



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

    mRecyclerView = findViewById(R.id.recyclerView);
    mSearchButton = findViewById(R.id.searchButton);
    mSearchField = findViewById(R.id.firebaseSearch);


    //mRecyclerView.setHasFixedSize(true);
    mSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //firebaseUserSearch(searchText);
            String searchText = mSearchField.getText().toString();

            Query firebaseSearchQuery = databaseReference.orderByChild("name")
                    .startAt(searchText)
                    .endAt(searchText + "\uf8ff");

            FirebaseRecyclerOptions<UserInfo> options = new FirebaseRecyclerOptions.Builder<UserInfo>()
                    .setQuery(firebaseSearchQuery, UserInfo.class).build();

            adapter = new FirebaseRecyclerAdapter<UserInfo, UsersViewHolder>(options) {

                @NonNull
                @Override
                public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_layout, parent, false);

                    return new UsersViewHolder(view);
                }

                @Override
                protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull UserInfo model) {
                    holder.setDetails(model.name, model.status);
                }
            };
            mRecyclerView.setLayoutManager(new LinearLayoutManager(SearchUserActivity.this));
            mRecyclerView.setAdapter(adapter);

        }
        });
    };


private void firebaseUserSearch(String searchText) {

}




// viewholder class

public class UsersViewHolder extends RecyclerView.ViewHolder {

    View mView;

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


    public void setDetails(String userName,String userStatus) {
        TextView name = mView.findViewById(R.id.name);
        TextView status = mView.findViewById(R.id.status);


        name.setText(userName);
        status.setText(userStatus);

        }

    }
}

我想在create方法中设置一个点击监听器以供内部搜索。但是,我无法在on监听器中初始化firebase适配器,因为在on start方法期间,我必须在on create方法中设置适配器开始监听。

0 个答案:

没有答案