简体   繁体   中英

Changing variable within Event Listener?

I am developing an android recipe app, User's have the option to only view vegan recipes. I am using Firebase as my Database where I am storing a variable "vegan", in my activity which displays the recipes I am retrieving the value of "vegan" from my database, which can be either "yes" or "no" (line: 54) and then the if statement (line: 65) checks if the user want's vegan recipes or not, however vegan = user.Vegan; does not seem to be changing the variable vegan, I know I am getting the value from the database but it won't change the value of vegan, can anyone tell me where I'm going wrong?

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());
                }
            });
        } 
    }
}

The problem is more than likely that onDataChange hasn't been called by time you check vegan in that if statement. Callbacks like that are asynchronous so you will need to wait for callback before performing any logic that's dependent on result.

In general what you're running in to is something many people moving to Firebase from SQL background encounter when trying to map over "joins" like this to the nested queries that Firebase requires. Probably outside scope of this particular question but use of RxJava makes managing set of operations like this much easier (for example that have async responses and 2nd query needs to use response of first one).

In your onStart() do something like 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; 

               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());
                   }
             });
            } 
         }

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


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM