Как также сохранить сгенерированный уникальный идентификатор в Firebase Realtime Databse - PullRequest
0 голосов
/ 11 января 2019

enter image description here Привет всем,

Я с нетерпением жду возможности сохранить уникальный идентификатор, сгенерированный при сохранении информации методом push в базе данных Firebase Realtime.

Ниже мой код

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

Он хранит всю информацию из textview, но в bookid он выглядит пустым, и я хочу сохранить нажимную клавишу "-LVx3qNb1HCxYKRE5cFl"

Класс "Мои книги"

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

}

Ответы [ 2 ]

0 голосов
/ 11 января 2019

Вы получили "" в вашей базе данных, потому что именно так вы создали экземпляр поля bookid в своем конструкторе. Чтобы решить эту проблему, измените в своем классе Books следующую строку кода:

this.bookid="";

до

this.bookid = bookid;
0 голосов
/ 11 января 2019

Ваш класс книг должен выглядеть следующим образом. Вы пропустили добавление значения параметра в строку bookid.

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

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...