【发布时间】:2018-07-24 17:23:03
【问题描述】:
我使用 Firebase DB 作为我的 Android 应用程序的数据库。我需要检查 db 是否包含具有名称的对象,该名称为用户提供。如果是,我将引用保存到它并加载数据,esle - 显示有关不正确的对象名称的信息。
作为我使用的示例: Firebase querying data
代码:
private void setListReferance(){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(Config.ADDLIST);
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton(Config.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String listName = input.getText().toString();
boolean correctName = checkListName(shoplistsRef, listName);
if(correctName){
listRef = shoplistsRef.child(listName);
myAdapter.setItemsListRef(listRef);
myAdapter.notifyDataSetChanged();
firebaseUpdate();
}
else{
String messege = "Niepoprawna nazwa listy";
Toast.makeText(MainActivity.this,
messege, Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton(Config.CANCEL, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
检查 db 是否包含的方法。对于测试,我添加了祝酒词。
private boolean checkListName(DatabaseReference ref, String listName){
boolean[] result = new boolean[1];
Query query = ref.child(listName);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(MainActivity.this,
ref.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,
dataSnapshot.getValue(String.class), Toast.LENGTH_LONG).show();
result[0] = true;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return result[0];
}
问题是当我写正确的名字(数据库有)时,第一个 toast 从 else 块 setListReferance() 显示(根本不应该显示),然后两个 toast 从 if (dataSnapshot.exists()){}
为什么会这样?我认为同步和异步方法存在问题,但我不知道如何解决。
【问题讨论】:
-
检查这里给出的答案stackoverflow.com/questions/34486417/… ...
-
您不能在
onDataChange()方法内部设置result[0],然后在外部简单地使用它。因此,请检查副本以了解您为什么会出现这种行为以及如何使用自定义回调来解决此问题。
标签: java android firebase firebase-realtime-database