我在addValueEventListener的dataSnapshot中遇到错误

时间:2018-06-27 09:09:54

标签: java

我的代码中包含此事件侦听器。我想获取Firebase中的值。但是在我的。getValue()方法中获取变量'ds'时无法识别。`

ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                showData(dataSnapshot);
            }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private void showData(DataSnapshot dataSnapshot) {

    for (DataSnapshot ds: dataSnapshot.getChildren());

    UserInformation uInfo = new UserInformation();

    uInfo.setAddress(ds.child(userID).getValue(UserInformation.class).getAddress());

    uInfo.setDate(ds.child(userID).getValue(UserInformation.class).getDate());

    uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());

    uInfo.setPhone(ds.child(userID).getValue(UserInformation.class).getPhone());

    uInfo.setQuantity(ds.child(userID).getValue(UserInformation.class).getQuantity());

    Log.d(TAG, "showData: address" + uInfo.getAddress());

    Log.d(TAG, "showData: date" + uInfo.getDate());

    Log.d(TAG, "showData: name" + uInfo.getName());

    Log.d(TAG, "showData: phone" + uInfo.getPhone());

    Log.d(TAG, "showData: quantity" + uInfo.getQuantity());

`我收到错误消息“找不到符号ds”

2 个答案:

答案 0 :(得分:0)

您要立即取消for循环:

 require 'send/PHPMailerAutoload.php';


  $mail = new PHPMailer();


//Enable SMTP debugging.

  $mail->SMTPDebug = 0;

//Set PHPMailer to use SMTP.

  $mail->isSMTP();

//Set SMTP host name

  $mail->Host = "smtp.gmail.com";

//Set this to true if SMTP host requires authentication to send email

  $mail->SMTPAuth = true;

//Provide username and password

  $mail->SMTPAuth = true;

//Provide username and password

  $mail->Username = "admin@admin.com";

  $mail->Password = "a23k234tbk";

//If SMTP requires TLS encryption then set it

  $mail->SMTPSecure = "tls";


//Set TCP port to connect to

  $mail->Port = 587;

  $mail->From = "admin@admin.com";

  $mail->FromName = "dummy.com";

  $mail->addAddress($user_email);

  $mail->isHTML();

  $mail->Subject = "dummy";

  $mail->Body = $table4 ? $table4 :"";



if($mail->send())
{

  header("Location:email_user.php?result=success");
  echo "";
}
else
{

  echo '';
}
}

您需要为for循环提供一个代码块,例如:

for (DataSnapshot ds: dataSnapshot.getChildren());

答案 1 :(得分:0)

dsfor循环的范围内定义,该循环的内容为空。您正在尝试在循环外访问它。

for (DataSnapshot ds: dataSnapshot.getChildren()) {

    UserInformation uInfo = new UserInformation();
    // ds is available here
    uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); 
    ...
}