嘿,我正在尝试从Firebase取得物品。我有一个setter和getter,我根据变量是否相等来设置true或false。即使变量相同,getter仍会向我返回false。 这是我在数据库中的内容:
这是一个getter和setter:
public boolean getSaved() {
return isSaved;
}
public void setSaved(boolean saved) {
isSaved = saved;
}
这是我从数据库获取数据的功能:
public void RegisterUser(final Data data, final Context c, final int position){
final ArrayList<String> Creator = new ArrayList<>(9);
final Registrazione reg = new Registrazione();
databaseReference.child("Torneo").addListenerForSingleValueEvent(new ValueEventListener() {
int i=0;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot DS: dataSnapshot.getChildren()){
if(i<= position){
Creator.add(i, DS.child("nameCreator").getValue(String.class));
i++;
}
}
Log.d("prova", Creator.get(position));
if (Creator.get(position) == "jj") {
reg.setSaved(false);
} else {
reg.setSaved(true);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是控件:
if (reg.getSaved()) {
Toast.makeText(c, "registration successful", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(c, "You are already registered", Toast.LENGTH_LONG).show();
}
感谢您的回答。
答案 0 :(得分:1)
在Java中,您需要使用:
if (Creator.get(position).equals("jj"))
不是:
if (Creator.get(position) == "jj")