gpt4 book ai didi

java - 更改事件监听器中的变量?

转载 作者:行者123 更新时间:2023-12-02 03:11:26 27 4
gpt4 key购买 nike

我正在开发一个 Android 食谱应用程序,用户可以选择仅查看素食食谱。我使用 Firebase 作为我的数据库,在其中存储变量“纯素食”,在我的 Activity 中显示食谱,我从数据库中检索“纯素食”的值,该值可以是"is"或“否”(行: 54) 然后 if 语句(第 65 行)检查用户是否想要素食食谱,但是 vegan = user.Vegan;似乎没有改变变量 vegan,我知道我从数据库中获取值,但它不会改变 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 语句中检查 vegan 时尚未调用 onDataChange。此类回调是异步的,因此您需要等待回调才能执行任何依赖于结果的逻辑。

一般来说,您遇到的情况是许多从 SQL 后台迁移到 Firebase 的人在尝试将这样的“联接”映射到 Firebase 所需的嵌套查询时遇到的情况。可能超出了这个特定问题的范围,但是使用 RxJava 使得管理这样的操作集变得更加容易(例如,具有异步响应并且第二个查询需要使用第一个查询的响应)。

关于java - 更改事件监听器中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com