2017-09-25 182 views
0

情况:我想从我的功能参数与网址从我的火力实时数据库中检索比较一网址。检索工作,但是,我不知道如何从我的if-else语句中的字符串比较中获取布尔值,其中我得到一个错误:“无法将值赋给最终变量'bool'”。如何从onDataChange方法获取数据?

比较()代码

private boolean compare(String url) { 
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch"); 
    final String newUrl = url; 
    final boolean bool = false; 

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      if (dataSnapshot.getChildrenCount() > 0) { 
       for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
        ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class); 
        if (executiveOrder.getUrl().equals(newUrl)) { 
         bool = true; 
        } else { 
         bool = false; 
        } 
       } 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.e("Executive Order", "The read failed: " + databaseError.getDetails()); 
     } 
    }); 

    return bool; 
} 

回答

0

您不能为最终变量分配一个新值。

如果您要访问内部类中的外部类变量,则必须声明为final。

一个简单但不优雅的方式是声明尺寸1.

mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch"); 
final String newUrl = url; 
final boolean[] bool = new boolean[1]; //boolean array of size 1 

mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     if (dataSnapshot.getChildrenCount() > 0) { 
      for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
       ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class); 
       if (executiveOrder.getUrl().equals(newUrl)) { 
        bool[0] = true; //access the bool value like this 
       } else { 
        bool[0] = false; 
       } 
      } 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     Log.e("Executive Order", "The read failed: " + databaseError.getDetails()); 
    } 
}); 

的布尔数组通过你是一个很大的错误的方式。你的代码会一直返回false。

Firebase数据库方法是异步的,意思是它们在一个单独的线程中执行,所以我们不能在从数据库中检索数据时做出决定。这取决于你的互联网的速度。

因此您必须调用onDataChange方法内的适当方法。示例...

private boolean compare(String url) { 
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch"); 
    final String newUrl = url; 

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      if (dataSnapshot.getChildrenCount() > 0) { 
       for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
        ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class); 
        boolean bool = executiveOrder.getUrl().equals(newUrl); 
        doSomething(bool); //This method will handle the logic 
       } 
      } 
     } 
     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.e("Executive Order", "The read failed: " + databaseError.getDetails()); 
     } 
    }); 
} 
0

错误告诉你是什么问题。您正试图将值分配给最终变量。尝试直接返回true或false。但请注意,Firebase调用是异步的,这意味着它可能会返回方法底部的默认值。

private boolean compare(String url) { 
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch"); 
    final String newUrl = url; 
    final boolean bool; 

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      if (dataSnapshot.getChildrenCount() > 0) { 
       for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
        ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class); 
        if (executiveOrder.getUrl().equals(newUrl)) { 
         bool = true; 
        } else { 
         bool = false; 
        } 
       } 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.e("Executive Order", "The read failed: " + databaseError.getDetails()); 
     } 
    }); 

    if(bool!=null)  < Add a condition to check if bool is null or not 
    return bool; 
    else return false; 
} 
+0

不能从void结果类型的方法返回true或false虽然 –

+0

您是否知道任何其他可能的方式返回true或false? –

+0

我改变了我的答案,现在应该没问题。但是我建议你提前做firebase检索,这样你就不需要阻止或等待结果。 –

0

使变量bool一个全局变量在你的类并删除final关键字,这样就可以同时读取和方法内的任意位置写入值。

相关问题