为什么我的if语句外的返回值为null?

时间:2019-06-09 09:13:27

标签: java android

我以为有人已经有这个问题了,但是没有一种解决方案对我有用。这是背景。我正在查询firestore以查看文档中是否存在某个索引。如果是这样(因此出现下面的if语句),则返回同一文档中的一个字段。 编辑:工作代码位于底部

我尝试将返回值声明为全局,局部,最终值,并且没有任何效果。

我正在记录结果,并在其中检查“项目图片1:”,我得到了要查找的url字符串,但是当我再次检查“项目图片2:”时,该值为null。值相同怎么办?

private String getImage(String classRestaurantId, final String menuItemName) {
        CollectionReference restaurantQuery = firebaseFirestore.collection("Restaurant_Data").document(classRestaurantId).collection("Menu_Card");
        restaurantQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                for (QueryDocumentSnapshot menuDocument : queryDocumentSnapshots) {
                    if (menuDocument.getString("menuItemName").equals(menuItemName)) {
                        orderItemImage = menuDocument.getString("menuItemImage");
                        Log.d(TAG, "item image 1:"+ orderItemImage);
                    }
                }
            }
        });
        Log.d(TAG, "item image 2:"+ orderItemImage);
        return orderItemImage;
    }

这是我的OnBindViewHolder:

public void onBindViewHolder(@NonNull final MealPlanDetailsAdapter.MealPlanDetailsViewHolder mpDetailsViewHolder, final int position) {



        // Menu items
        mpDetailsViewHolder.mealplanMenuRecycler.setHasFixedSize(true);
        mpDetailsViewHolder.mealplanMenuRecycler.setLayoutManager(new LinearLayoutManager(adapterContext, LinearLayoutManager.HORIZONTAL, false));

        String orderId = mealplanDetailsArrayList.get(position).getOrderDetails();

        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        if (firebaseUser != null) {
            for (UserInfo profile : firebaseUser.getProviderData()) {
                userId = profile.getUid();
            }
        }

        // Get order details
        final String restaurantId = mealplanDetailsArrayList.get(position).getRestaurantId();
        CollectionReference orderReference = firebaseFirestore.collection("Customer_Data").document(userId).collection("Meal_Plans").document(orderId).collection("Placed_Order");
        orderReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                mealplanMenuArrayList = new ArrayList<>();
                for (QueryDocumentSnapshot orderDocument : queryDocumentSnapshots) {
                    // Get image
                    final String menuItemName = orderDocument.getString("menuItemName");


                    //String menuItemImage = getImage(restaurantId, menuItemName);

                    // Start of image collection
                    CollectionReference restaurantQuery = firebaseFirestore.collection("Restaurant_Data").document(restaurantId).collection("Menu_Card");
                    restaurantQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                            for (QueryDocumentSnapshot menuDocument : queryDocumentSnapshots) {
                                if (menuDocument.getString("menuItemName").equals(menuItemName)) {
                                    orderItemImage = menuDocument.getString("menuItemImage");
                                    Log.d(TAG, "item image 1:"+ orderItemImage);
                                }
                            }
                        }
                    });
                    // End of image collection


                    MealplanMenu mealplanMenu = new MealplanMenu(menuItemName, orderDocument.getString("menuItemQuantity"),orderItemImage);
                    mealplanMenuArrayList.add(mealplanMenu);
                }
                mealplanMenuAdapter = new MealplanMenuAdapter(adapterContext, mealplanMenuArrayList);
                mpDetailsViewHolder.mealplanMenuRecycler.setAdapter(mealplanMenuAdapter);
            }
        });
    }

这是ElektroWeak的最终有效代码。如果人们没有为从Firestore查询数据的嵌套回收站视图寻找类似的解决方案,我会感到惊讶。

final String restaurantId = mealplanDetailsArrayList.get(position).getRestaurantId();
        CollectionReference orderReference = firebaseFirestore.collection("Customer_Data").document(userId).collection("Meal_Plans").document(orderId).collection("Placed_Order");
        orderReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                final ArrayList<MealplanMenu> mealplanMenuArrayList = new ArrayList<>();
                for (QueryDocumentSnapshot orderDocument : queryDocumentSnapshots) {
// Get image
                    final String menuItemName = orderDocument.getString("menuItemName");
                    final String menuItemQuantity = orderDocument.getString("menuItemQuantity");

//String menuItemImage = getImage(restaurantId, menuItemName);

                    CollectionReference restaurantQuery = firebaseFirestore.collection("Restaurant_Data").document(restaurantId).collection("Menu_Card");
                    restaurantQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                            for (QueryDocumentSnapshot menuDocument : queryDocumentSnapshots) {
                                if (menuDocument.getString("menuItemName").equals(menuItemName)) {
                                    String menuItemImage = menuDocument.getString("menuItemImage");
                                    Log.d(TAG, "item image 1:"+ orderItemImage);

                                    MealplanMenu mealplanMenu = new MealplanMenu(menuItemName, menuItemQuantity, menuItemImage);
                                    mealplanMenuArrayList.add(mealplanMenu);
                                }
                            }
                            mealplanMenuAdapter = new MealplanMenuAdapter(adapterContext, mealplanMenuArrayList);
                            mpDetailsViewHolder.mealplanMenuRecycler.setAdapter(mealplanMenuAdapter);
                        }

                    });

// Log.d(TAG, "item image 2:"+ orderItemImage);
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

restaurantQuery.get().addOnSuccessListener(...);

这里您要为查询添加一个侦听器。这意味着addOnSuccessListener将立即返回,但是稍后在后台执行查询并返回结果时,将调用onSuccess。因此,此代码将在调用onSuccess之前执行:

Log.d(TAG, "item image 2:"+ orderItemImage);
return orderItemImage;

,这意味着orderItemImage仍然是null,因为它不是通过onSuccess方法设置的。您可以将依赖于orderItemImage的代码放在onSuccess方法内:

restaurantQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        for (QueryDocumentSnapshot menuDocument : queryDocumentSnapshots) {
            if (menuDocument.getString("menuItemName").equals(menuItemName)) {
                orderItemImage = menuDocument.getString("menuItemImage");
                Log.d(TAG, "item image 1:"+ orderItemImage);

                // Place your code here, or call methods that depend on orderItemImage
            }
        }
    }
});

编辑:

这两行取决于orderItemImage

MealplanMenu mealplanMenu = new MealplanMenu(menuItemName, orderDocument.getString("menuItemQuantity"),orderItemImage);
mealplanMenuArrayList.add(mealplanMenu);

因此您可以将它们放置在onSuccess内以进行餐厅查询,也可以将适配器创建代码移到那里{p>

CollectionReference restaurantQuery = firebaseFirestore.collection("Restaurant_Data").document(restaurantId).collection("Menu_Card");
restaurantQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        for (QueryDocumentSnapshot menuDocument : queryDocumentSnapshots) {
            if (menuDocument.getString("menuItemName").equals(menuItemName)) {
                orderItemImage = menuDocument.getString("menuItemImage");
                Log.d(TAG, "item image 1:"+ orderItemImage);

                MealplanMenu mealplanMenu = new MealplanMenu(menuItemName, orderDocument.getString("menuItemQuantity"),orderItemImage);
                mealplanMenuArrayList.add(mealplanMenu);
            }
        }
        mealplanMenuAdapter = new MealplanMenuAdapter(adapterContext, mealplanMenuArrayList);    
        mpDetailsViewHolder.mealplanMenuRecycler.setAdapter(mealplanMenuAdapter);
    }
});