使用FirebaseRecyclerAdapter进行Firebase搜索(Firebase用户界面3.2.2)

时间:2018-03-02 15:10:05

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

我有一个片段,在其中使用RecyclerView我显示来自Firebase DB的所有对象。在同一个片段中,我有一个EditText。

目标:用户可以在Firebase数据库中进行搜索。搜索结果应填充在相同的 RecyclerView 中,其中最初显示DB中的所有对象。出于这个原因,我使用 FirebaseUI版本3.2.2 提供的 FirebaseRecyclerAdapter 。为了达到这个结果,我正在关注这个YouTube教程https://www.youtube.com/watch?v=b_tz8kbFUsU

问题:结果未显示且搜索功能无效。

这是我的代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_best_buys, container, false);

    recView = view.findViewById(R.id.fragment_best_buys_recycler_view_two);
    databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);

        // Edit Text Listener
    mEditText = view.findViewById(R.id.fragment_best_buys_edit_text);
    Button test = view.findViewById(R.id.faccia_search);
    test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String searchText = mEditText.getText().toString();
            firebaseUserSearch(searchText);
        }
    });

    return view;
}

private void firebaseUserSearch(String searchText) {

    System.out.print("I am searching: " + searchText);

    Toast.makeText(getActivity(), "Started Search " + searchText, Toast.LENGTH_LONG).show();

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

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

    FirebaseRecyclerAdapter<BestBuysProducts, BestBuysViewHolder> adapter = new FirebaseRecyclerAdapter<BestBuysProducts, BestBuysViewHolder>(options) {
        @Override
        public BestBuysViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_best_buys, parent, false);
            return new BestBuysViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull BestBuysViewHolder holder, int position, @NonNull BestBuysProducts model) {
            holder.setDetails(getContext(), model.getProduct_name());

        }};

    recView.setLayoutManager(new LinearLayoutManager(getContext()));
    recView.setAdapter(adapter);

}

查看持有人类

public static class BestBuysViewHolder extends RecyclerView.ViewHolder {

    View mView;

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


    public void setDetails(final Context ctx, final String productName) {
        TextView titleTextView = itemView.findViewById(R.id.item_best_buys_title);
        titleTextView.setText(productName);
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

{
  "compilerOptions": {
    "outDir": "./moonholdings/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react"
  },
  "include": [
    "./app/**/*"
  ]
}