使用搜索栏在Firebase中搜索数据

时间:2018-10-14 20:44:51

标签: android firebase firebase-realtime-database firebaseui

我有一个材质搜索栏,用于在Firebase中进行搜索。此搜索基于电话号码返回值。当我搜索Firebase中不存在的电话号码时,它不会返回任何错误,但是每当我搜索与Firebase中的电话号码匹配的电话号码时,都会出现以下错误。对此有帮助吗?

错误

  

java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.TextView.setText(java.lang.CharSequence)'上的null   对象引用

材料搜索条形码

public void onSearchConfirmed(CharSequence text) {
            //when search finish
            //Show result of search adapter
            startSearch(text.toString());
        }

搜索代码

private void startSearch(String text) {
    //create query by name
    Query searchByName = requests.orderByChild("phone").equalTo(text);
    //Create Options with query
    FirebaseRecyclerOptions<Request> foodOptions = new FirebaseRecyclerOptions.Builder<Request>()
            .setQuery(searchByName, Request.class)
            .build();

    Searchadapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(foodOptions) {
        @Override
        protected void onBindViewHolder(@NonNull final OrderViewHolder viewHolder, final int position, @NonNull final Request model) {
            viewHolder.txtOrderId.setText(Searchadapter.getRef(position).getKey());
            viewHolder.txtOrderStatus.setText(convertCodeToStatus(model.getStatus()));
            viewHolder.txtOrderPhone.setText(model.getPhone());
            viewHolder.txtDateTime.setText(model.getDateTime());
}
        @Override
        public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.food_item, parent, false);
            return new OrderViewHolder(itemView);
        }
    };
    Searchadapter.startListening();
    recyclerView.setAdapter(Searchadapter); //Set adapter for Recycler View

Firebase结构

enter image description here

0 个答案:

没有答案