gpt4 book ai didi

java - 如何知道 Firebase 中是否有获取的数据?

转载 作者:行者123 更新时间:2023-12-02 04:35:43 25 4
gpt4 key购买 nike

我正在尝试寻找是否有办法知道 Firebase 实时数据库中是否已提取数据。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// ARRAY
accountList = new ArrayList<>();
// Assigning Object to Controls (bullshit)
textboxUser = (TextView) findViewById(R.id.textBoxUsername);
textboxPass = (TextView) findViewById(R.id.textBoxPassword);
buttonEnter = (Button) findViewById(R.id.buttonEnter);

postRef = FirebaseDatabase.getInstance().getReference("account");
dataRefaccount = FirebaseDatabase.getInstance().getReference("account");


// OnClick Method Button
buttonEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

try {
username = textboxUser.getText().toString();
Query query = FirebaseDatabase.getInstance().getReference("account").orderByChild("username").equalTo(username);
query.addListenerForSingleValueEvent(valueEventListener);
} catch (Exception e) {
e.printStackTrace();
} finally {
Toast.makeText(getApplicationContext(), getted, Toast.LENGTH_LONG).show();
}



}
});
}

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

for (DataSnapshot data : dataSnapshot.getChildren()) {
if (data.child(username).exists()) {
getted = "yes";
} else {
getted = "No";
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
};

setter/getter 始终为“NO”。

最佳答案

你应该尝试一下这个。首先确保您在查询响应中没有收到任何错误。

Query query = FirebaseDatabase.getInstance().getReference().child("account").orderByChild("username").equalTo(username);

ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null && dataSnapshot.getChildrenCount() > 0) {
for (DataSnapshot data : dataSnapshot.getChildren()) {

// I'm sure you are looking for below line.. Go and try this.
if (data.child("username").getValue().toString().equal(username) ) {
getted = "yes";
break;
} else {
getted = "No";
}
}
} else {
getted = No value available;
}
Toast.makeText(getApplicationContext(), "getted : " + getted, Toast.LENGTH_LONG).show();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
};

仅供引用:Firebase 调用是异步调用,因此单击按钮时的 finally 子句将始终在 Firebase 响应之前执行。因此,尝试从那里删除 try-catch 并使用上面的代码来打印获取的值。

关于java - 如何知道 Firebase 中是否有获取的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553719/

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