我正在开发一个Android食谱应用,用户可以选择仅查看纯素食食谱。我正在使用Firebase作为数据库,在其中存储变量“素食主义者”的活动中,该活动显示了配方,我正在从数据库中检索“素食主义者”的值,该值可以是“是”或“否”(行:54),然后执行if语句(第65行),检查用户是否想要素食主义者的菜谱,但是vegan = user.Vegan;似乎并没有改变素食主义者的变量,我知道我从数据库中获取了价值,但它不会改变素食主义者的价值,有人可以告诉我我要去哪里吗?

public class SwipeActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private DatabaseReference mRecipeReference;
    private DatabaseReference newRef;
    private DatabaseReference myRef3;
    private DatabaseReference veganRef;
    private TextView editTextName;
    private TextView editTextCategory;
    private ImageView profileImageView;
    private ImageButton Back;
    private ImageButton Like;
    private ImageButton Dislike;
    private DatabaseReference databaseReference;
    private DatabaseReference userRef;
    String imgURL;
    String recipeKey;
    Map<String, Recipe> likedRecipes = new HashMap<String,Recipe>();
    String user = FirebaseAuth.getInstance().getCurrentUser().getUid();


    String vegan = "no";  //Here is the variable declaration

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

        databaseReference = FirebaseDatabase.getInstance().getReference();
        userRef = FirebaseDatabase.getInstance().getReference().child("user").child(user);
        mRecipeReference = FirebaseDatabase.getInstance().getReference().child("recipe");

        editTextName = (TextView) findViewById(R.id.editTextName);
        editTextCategory = (TextView) findViewById(R.id.editTextCategory);
        profileImageView = (ImageView) findViewById(R.id.profileImageView);
        Back = (ImageButton) findViewById(R.id.Back);
        Back.setOnClickListener(this);
        Like = (ImageButton) findViewById(R.id.Like);
        Like.setOnClickListener(this);
        Dislike = (ImageButton) findViewById(R.id.Dislike);
        Dislike.setOnClickListener(this);

    }

    @Override
    public void onStart() {
        super.onStart();

        ValueEventListener userListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                User user = dataSnapshot.getValue(User.class);
                vegan = user.Vegan; //Here I am retrieving the string from firebase database, which is either "yes" or "no"
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
                // ...
            }
        };
        userRef.addValueEventListener(userListener);

        if (vegan == "yes") {  //Here I am checking if the user is vegan or not
            veganRef = databaseReference.child("recipe");

            veganRef.orderByChild("Category").equalTo("Vegan").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot recipeSnapshot : dataSnapshot.getChildren()) {
                        Recipe recipe = recipeSnapshot.getValue(Recipe.class);
                        recipeKey = recipeSnapshot.getKey();
                        editTextName.setText(recipe.Name + ", " + recipe.Calories);
                        editTextCategory.setText(recipe.Category);
                        imgURL = recipe.Img;

                        Picasso.with(getApplicationContext()).load(imgURL)//download URL
                                .placeholder(R.drawable.placeholder_image)//use default image
                                .error(R.drawable.placeholder_image)//if failed
                                .into(profileImageView);//imageview

                        likedRecipes.put(recipeKey, recipe);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
                }
            });
        }
    }
}

最佳答案

问题很可能是您在该if语句中检查onDataChange时尚未调用vegan的原因。像这样的回调是异步的,因此您需要先等待回调,然后再执行任何依赖结果的逻辑。

通常,您要运行的是许多人从SQL后台迁移到Firebase时遇到的问题,它们试图将此类“联接”映射到Firebase所需的嵌套查询。可能不在这个特定问题的范围之内,但是使用RxJava使得管理这样的操作集变得容易得多(例如,具有异步响应并且第二个查询需要使用第一个查询的响应)。

09-10 08:56