简体   繁体   中英

How can I check wishlist data is empty or not in firebase database

I want to check "Wishlist" under User UID is empty or not because if "Wishlist" is empty I am going to create the "Wishlist 2" in it. How can I check this "Wishlist is empty or not in my firebase database.

The concept is like this - check wishlist is empty or not, if wishlist is empty then I will create another wishlist 2 in database. Wishlist is not default it will only create if user add eg item 1 in wishlist and then if user already add item 1 in wishlist. I will create wishlist 2 for his item 2

这是我的 firebase 数据库的屏幕截图1

Here is my code

public class web_view extends AppCompatActivity {
    //for fav
    FloatingActionButton fab;

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_v);

        //fab start here
        
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String passURL = getIntent().getStringExtra("key");
                if (passURL.matches("[0-9]+") && passURL.length()>= 10){ ////if fav book input is ISBN number
                    FirebaseDatabase.getInstance().getReference("Users")
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Wishlist")// which child goes to under the current user UID
                            .setValue("http://www.librarything.com/isbn/" + passURL).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                Toast.makeText(web_view.this, "Saved to Wishlist", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }else {//if wish list book is no ISBN
                    Toast.makeText(web_view.this, "Can't Save", Toast.LENGTH_LONG).show();
                }
            }
        });
}
}

Okay, you can do it with .push() which will autogenerate ID for each of your elements inside the Wishlist. So you can do it like this:

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstace().getCurrentUser().getUid()).child("Wishlist");
myRef.push().setValue(newWishlistString);

This will generate something like this in Firebase:

在此处输入图像描述

Then you can just retrieve it like this:

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstace().getCurrentUser().getUid()).child("Wishlist");

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
               //do something here with data
    }

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

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM