在firebase查询中没有显示Toast?

时间:2018-02-18 20:27:38

标签: java android firebase-realtime-database

toast没有在firebase查询中显示? 如果用户发现它显示用户列表,但是当用户没有找到它时没有显示toast。我想在找不到用户时显示Toast。我的代码是:

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            Iterable < DataSnapshot > children = dataSnapshot.getChildren();

            for (DataSnapshot child: children) {
                User value = child.getValue(User.class);
                if (value.getCity().equals(city) && value.isDonor()) {
                    data.add(value);
                    keyList.add(child.getKey());
                    adapter.notifyDataSetChanged();
                }
            }
        } else {
            Toast.makeText(AdminSearchview.this, "Donors not Found!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这是Json片段。现在测试会更清楚。现在你可以看到我发布的整个json文件。希望找到更好的解决方案。

{
  "users" : {
"1aduZZLM15e7BfVLMb66ZUw9lS82" : {
  "blood_Group" : "A+",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "asghar@yahoo.com",
  "gender" : "Male",
  "mobile" : "03652325554",
  "name" : "Asghar",
  "password" : "asghar",
  "uid" : "1aduZZLM15e7BfVLMb66ZUw9lS82"
},
"5aqisXp6MGVczHQpot3YmcTcLjW2" : {
  "blood_Group" : "B+",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "irfan@yahoo.com",
  "gender" : "Male",
  "mobile" : "03066521016",
  "name" : "Irfan",
  "password" : "fani12",
  "uid" : "5aqisXp6MGVczHQpot3YmcTcLjW2"
},
"K3ERR6YBIFSqrEP1SBL8fvh39J22" : {
  "blood_Group" : "O+",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "abdullah@yahoo.com",
  "gender" : "Male",
  "mobile" : "03067908636",
  "name" : "Abdullah",
  "password" : "abdullah",
  "uid" : "K3ERR6YBIFSqrEP1SBL8fvh39J22"
},
"NfnrLN3B68Ty0oOCpAxOSqNhVL53" : {
  "blood_Group" : "AB+",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "bilal@yahoo.com",
  "gender" : "Male",
  "mobile" : "03167846423",
  "name" : "Bilal",
  "password" : "bilal12",
  "uid" : "NfnrLN3B68Ty0oOCpAxOSqNhVL53"
},
"Q8LWIShbfcdIeixwGQbtFh99UgT2" : {
  "blood_Group" : "AB-",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "arslan@yahoo.com",
  "gender" : "Male",
  "mobile" : "03227637423",
  "name" : "Arslan",
  "password" : "arslan",
  "uid" : "Q8LWIShbfcdIeixwGQbtFh99UgT2"
},
"RqYsc92qBYhMbMexa7jMrrMEqU83" : {
  "blood_Group" : "A-",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "arshad@yahoo.com",
  "gender" : "Male",
  "mobile" : "03214569842",
  "name" : "Arshad",
  "password" : "arshad",
  "uid" : "RqYsc92qBYhMbMexa7jMrrMEqU83"
},
"Sg6KYzywzXRi7s8M6lcyfQUCHSf2" : {
  "blood_Group" : "B-",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "asif@yahoo.com",
  "gender" : "Male",
  "mobile" : "03007508236",
  "name" : "Asif",
  "password" : "asif12",
  "uid" : "Sg6KYzywzXRi7s8M6lcyfQUCHSf2"
},
"TRqlMF1aAkgJCL8VBEIJbGEJD1j2" : {
  "admin" : true,
  "blood_Group" : "B+",
  "city" : "Faisalabad",
  "donor" : false,
  "email" : "admin@admin.com",
  "gender" : "Male",
  "mobile" : "03046091706",
  "name" : "Admin",
  "password" : "admin1",
  "uid" : "TRqlMF1aAkgJCL8VBEIJbGEJD1j2"
},
"XitGtEPpvfgq2Atx54nBEdagfMs1" : {
  "blood_Group" : "B+",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "salman@email.com",
  "gender" : "Male",
  "mobile" : "03215467945",
  "name" : "Salman",
  "password" : "salman",
  "uid" : "XitGtEPpvfgq2Atx54nBEdagfMs1"
},
"tg2X3LrtfRN0TT8bM369qj7O5Nf1" : {
  "blood_Group" : "O-",
  "city" : "Faisalabad",
  "donor" : true,
  "email" : "rizwan@yahoo.com",
  "gender" : "Male",
  "mobile" : "03128968656",
  "name" : "Rizwan",
  "password" : "rizwan",
  "uid" : "tg2X3LrtfRN0TT8bM369qj7O5Nf1"
}
}}

3 个答案:

答案 0 :(得分:0)

我认为dataSnapshot.getChildren().size()总是如此。尝试检查dataSnapshot.exists()而不是<?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; //Load composer's autoloader require 'vendor/autoload.php'; $mail = new PHPMailer(true); // Passing `true` enables exceptions try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); //Attachments $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } ,如果大小为0则显示Toast

答案 1 :(得分:0)

您可以使用此方法显示Toast

 query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Iterable < DataSnapshot > children = dataSnapshot.getChildren();

                for (DataSnapshot child: children) {
                    User value = child.getValue(User.class);
                    if (value.getCity().equals(city) && value.isDonor()) {
                        data.add(value);
                        keyList.add(child.getKey());
                        adapter.notifyDataSetChanged();
                    }else {
                Toast.makeText(AdminSearchview.this, "Donors not Found!", Toast.LENGTH_SHORT).show();
            }
                }
            } 
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 2 :(得分:0)

最后我通过添加此行代替else语句解决了问题。

                if (data.size() == 0){
                    Toast.makeText(AdminSearchview.this, "Donors not Found!",Toast.LENGTH_SHORT).show();
                }