在firestore中查询后未显示数据

9w11ddsr  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(159)
public class ShowingParticularProducts extends AppCompatActivity {
    FirebaseFirestore mFirebaseFireStore;
    private FirestoreRecyclerAdapter<ProductModel, showingParticularProductAdapter.showingParticularProductViewHolder> adapter;
    public RecyclerView mRecyclerView;
    List<String> AllProductName=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showing_particular_products);
        mRecyclerView = findViewById(R.id.RecyclerViewParticularProducts);
        mFirebaseFireStore = FirebaseFirestore.getInstance();
        ImageView returnHome = findViewById(R.id.ImageViewHome);
        ImageView Cart=findViewById(R.id.ImageViewCartParticularProducts);
        String SearchText=getIntent().getStringExtra("SearchName");

            loadAllProductName();

        if(SearchText != null) {
        List<String> suggest=new ArrayList<>();
        for(String search:AllProductName) {
            if (search.toLowerCase().contains(SearchText)) {
                suggest.add(search.toLowerCase());
            }
        }
            Query query1 = mFirebaseFireStore.collection("Products").whereIn("SearchID", Collections.singletonList(suggest));
            FirestoreRecyclerOptions<ProductModel> options1 = new FirestoreRecyclerOptions.Builder<ProductModel>().setQuery(query1, ProductModel.class).build();
            adapter = new showingParticularProductAdapter(options1);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.setAdapter(adapter);

    }

        returnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ShowingParticularProducts.this, HomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });
        Cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cartActivity=new Intent(ShowingParticularProducts.this,CartActivity.class);
                startActivity(cartActivity);
            }
        });
    }

    private void loadAllProductName() {
        mFirebaseFireStore=FirebaseFirestore.getInstance();
        Query query2=mFirebaseFireStore.collection("Products");
        query2.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for(QueryDocumentSnapshot queryDocumentSnapshot: Objects.requireNonNull(task.getResult())){
                        AllProductName.add(queryDocumentSnapshot.getString("Name"));
                    }
                }
            }
        });
    }

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

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

在firestore中使用loop with the field查询每个数据后,recyclerview中没有显示任何数据。首先我已经加载了一个数组列表中的所有产品名称,然后我查询每个名称是否包含搜索文本,如果包含,则根据recyclerview中的数据显示所有数据。希望大家都清楚这一点。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题