从通过 singleSnapshot 获取值的 class 对象使用 getter 函数时出现问题。 Android,Java
Problem in using getter function from a class object that gets value through singleSnapshot . Android,Java
我正在通过单个快照(存储在 'Advertisement' class 对象中)从 firebase 中提取用户输入的数据。
产品名称的getter方法有效,但是getProductDescription() returns一个空值,我不知道为什么。即,填充了 textView14 而不是 textView15。
数据已正确上传到firebase控制台。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_new, container, false);
// productImage = (ImageView) v.findViewById(R.id.imageView4);
//productDescription = (TextView) v.findViewById(R.id.textView15);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reff;
productName = v.findViewById(R.id.textView14);
productDescription = v.findViewById(R.id.textView15);
reff = database.getReference();
reff.child("Advertisement").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot;
Advertisement advertisement = snapshot.child("adv").getValue(Advertisement.class);
productName.setText(advertisement.getProductName());
productDescription.setText( advertisement.getProductDescription());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return v;
}
Enter image description here
This is the firebase console
package in.revmeup.revmeup;
import androidx.annotation.Keep;
@Keep
public class Advertisement {
private String productName;
private String productDescription;
private String productImageUrl;
public Advertisement(String productDescription)
{
this.productName=productName;
this.productImageUrl=productImageUrl;
this.productDescription=productDescription;
}
public Advertisement()
{
}
@Override
public String toString() {
return "Advertisement{" +
"product_Name='" + productName + '\'' +
", product_Image_URL='" + productImageUrl + '\'' +
", product_Description='" + productDescription +
'}';
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public String getProductImageUrl() {
return productImageUrl;
}
public void setProductImageUrl(String productImageUrl) {
this.productImageUrl = productImageUrl;
}
}
请帮我解决这个问题。这让我特别烦恼,因为一个函数有效而另一个无效,而且它们属于同一个对象。
问题已得到纠正。我没有向数据库中添加广告对象 class,而是添加了哈希映射值(该代码位在另一个 Java 文件中,我完全没有考虑一下)。这是一个有趣的(对我来说很愚蠢)错误,但我对我的第一个 Whosebug 问题并不后悔!
我正在通过单个快照(存储在 'Advertisement' class 对象中)从 firebase 中提取用户输入的数据。
产品名称的getter方法有效,但是getProductDescription() returns一个空值,我不知道为什么。即,填充了 textView14 而不是 textView15。
数据已正确上传到firebase控制台。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_new, container, false);
// productImage = (ImageView) v.findViewById(R.id.imageView4);
//productDescription = (TextView) v.findViewById(R.id.textView15);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reff;
productName = v.findViewById(R.id.textView14);
productDescription = v.findViewById(R.id.textView15);
reff = database.getReference();
reff.child("Advertisement").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot;
Advertisement advertisement = snapshot.child("adv").getValue(Advertisement.class);
productName.setText(advertisement.getProductName());
productDescription.setText( advertisement.getProductDescription());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return v;
}
Enter image description here
This is the firebase console
package in.revmeup.revmeup;
import androidx.annotation.Keep;
@Keep
public class Advertisement {
private String productName;
private String productDescription;
private String productImageUrl;
public Advertisement(String productDescription)
{
this.productName=productName;
this.productImageUrl=productImageUrl;
this.productDescription=productDescription;
}
public Advertisement()
{
}
@Override
public String toString() {
return "Advertisement{" +
"product_Name='" + productName + '\'' +
", product_Image_URL='" + productImageUrl + '\'' +
", product_Description='" + productDescription +
'}';
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public String getProductImageUrl() {
return productImageUrl;
}
public void setProductImageUrl(String productImageUrl) {
this.productImageUrl = productImageUrl;
}
}
请帮我解决这个问题。这让我特别烦恼,因为一个函数有效而另一个无效,而且它们属于同一个对象。
问题已得到纠正。我没有向数据库中添加广告对象 class,而是添加了哈希映射值(该代码位在另一个 Java 文件中,我完全没有考虑一下)。这是一个有趣的(对我来说很愚蠢)错误,但我对我的第一个 Whosebug 问题并不后悔!