redface
redface

Reputation: 325

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

enter image description hereHello 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;
}

}

Upvotes: 0

Views: 780

Answers (2)

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;
}

}

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

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;

Upvotes: 1

Related Questions