简体   繁体   中英

How to also save the Generated unique ID in Firebase Realtime Databse

在此处输入图片说明 Hello Everyone,

I am looking forward to save the unique id generated when saving the information using push method in Firebase Realtime database.

Below is my code

String bookid = dbreference.child("books").child(condition).push().getKey();

                Toast.makeText(getApplicationContext(), "Your question will be uploaded shortly !", Toast.LENGTH_SHORT).show();
                b = new Books(btitle, bauthor, bdesc, condition, sellername, selleremail, sellermobile,  selleruniversity, bprice,bookid);


                dbreference.child("books").child(condition).child(bookid).setValue(b);

It stores all the information from textview but in bookid it appears empty which I want to store push key which is "-LVx3qNb1HCxYKRE5cFl"

My Books class is

public class Books {
public String bname;
public String bauthor;
public String bdesc;
public String sellername;
public String selleremail;
public String sellermobile;
public String category;
public String selleruniversity;
public String bcondition;
public double price;
public String pics;
 public String bookid;
//  public Uri uri;

public Books(String bname, String bauthor, String bdesc, String bcondition, String sellername, String selleremail, String sellermobile,  String selleruniversity, double price, String bookid) {
    this.bname = bname;
    this.bauthor = bauthor;
    this.bdesc = bdesc;
    this.sellername = sellername;
    this.selleremail = selleremail;
    this.sellermobile = sellermobile;
    this.category = category;
    this.selleruniversity = selleruniversity;
    this.bcondition = bcondition;
    this.price = price;
    this.pics="";
     this.bookid="";
    //   this.uri = uri;
}

public Books(){}
public String getBname() {
    return bname;
}
public String getBauthor() {
    return bauthor;
}
public String getBdesc() {
    return bdesc;
}
public String getSellername() {
    return sellername;
}
public String getSelleremail() {
    return selleremail;
}
public String getSellermobile() { return sellermobile;}
public String getCategory() {
    return category;
}
public String getSelleruniversity() {
    return selleruniversity;
}
public String getCondition() { return bcondition;}
public Double getPrice() {
    return price;
}
public String getBookid() {
    return bookid;
}
public String getPics() {
    return pics;
}
public void setPics(String pics) {
    this.pics = pics;
}

}

Your books class should look like this. You missed adding the parameter's value to your bookid String.

public class Books {

public String bname;
public String bauthor;
public String bdesc;
public String sellername;
public String selleremail;
public String sellermobile;
public String category;
public String selleruniversity;
public String bcondition;
public double price;
public String pics;
public String bookid;
//public Uri uri;

public Books(String bname, String bauthor, String bdesc, String bcondition, String sellername, String selleremail, String sellermobile,  String selleruniversity, double price, String bookid) {
this.bname = bname;
this.bauthor = bauthor;
this.bdesc = bdesc;
this.sellername = sellername;
this.selleremail = selleremail;
this.sellermobile = sellermobile;
this.category = category;
this.selleruniversity = selleruniversity;
this.bcondition = bcondition;
this.price = price;
this.pics = "";
this.bookid = bookid;
//this.uri = uri;
}

public Books(){}

public String getBname() {
    return bname;
}
public String getBauthor() {
    return bauthor;
}
public String getBdesc() {
    return bdesc;
}
public String getSellername() {
    return sellername;
}
public String getSelleremail() {
    return selleremail;
}
public String getSellermobile() { return sellermobile;}
public String getCategory() {
    return category;
}
public String getSelleruniversity() {
    return selleruniversity;
}
public String getCondition() { return bcondition;}
public Double getPrice() {
    return price;
}
public String getBookid() {
    return bookid;
}
public String getPics() {
    return pics;
}
public void setPics(String pics) {
    this.pics = pics;
}

}

You got "" in your database because this is how you have instantieted the bookid field in your constructor. To solve this, please change in your Books class, the following line of code:

this.bookid="";

to

this.bookid = bookid;

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