2016-12-06 10 views
0

私はアンドロイドレシピアプリを開発していますが、ユーザーにはビーガンレシピのみを表示するオプションがあります。私は自分のデータベースで "vegan"という変数を格納しているデータベースとしてFirebaseを使用しています。私のデータベースで "vegan"の値を取得しているレシピを表示しています( "yes"または "no" :54)、そしてif文(行:65)は、ユーザがveganのレシピを望むかどうかをチェックしますが、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()); 
       } 
      }); 
     } 
    } 
} 
+0

ような何かを行います。したがって、応答が来た後にonDataChangeメソッド内でそのタスクを実行できますか? – Raghavendra

+1

ifステートメントを 'onDataChange'メソッドの中に置くと、動作する可能性があります –

答えて

2

問題はonDataChangeがステートメント場合は、その中にveganをチェックする時間によって呼び出されていない可能性が高い以上のものです。そのようなコールバックは非同期なので、結果に依存するロジックを実行する前にコールバックを待つ必要があります。

一般的には、Firebaseに必要なネストされたクエリにこのような「結合」をマップしようとすると、多くの人がSQLバックグラウンドでFirebaseに移動することになります。おそらくこの特定の質問の範囲外ですが、RxJavaを使用すると、このような操作の管理が非常に簡単になります(非同期応答と2番目のクエリでは最初の応答を使用する必要があります)。 JohnO'Reillyはそれが来て、応答まで更新するために、ある程度の時間が必要があると述べ@としてごONSTARTで

0

()この

@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); 


    } 
関連する問題