所以我得到错误,当我尝试上传数据在firebase.我想在以下格式
我尝试做更改,它第一次做了,但是......几天后,它向我显示错误。
我想我必须在这里给予更多的细节。所以基本上我是在尝试上传数据
Upload Data before scrolling down
Next Part after scroll down
但只要我点击保存按钮,它给予我以下错误error
我正在使用Firebase实时数据库和Firebase存储
这是一些代码:
textActivity.java(用于上传数据和img的代码)
public class textActivity extends AppCompatActivity {
TextView ProductName, ProductBrand, ProductExpiry, ProductQty, ProductCalories, ProductSugar, ProductFibers, ProductCarbs;
ImageView uploadbtn, productImage;
Button submit;
Uri ImageUri;
RelativeLayout relativeLayout;
ProgressDialog dialog;
BottomNavigationView navigationView;
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
final Calendar myCalendar = Calendar.getInstance();
private FirebaseDatabase database;
// private DatabaseReference reference;
private FirebaseStorage firebaseStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_text);
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...")
dialog.setCancelable(false);
dialog.setTitle("Product Uploading");
dialog.setCanceledOnTouchOutside(false);
uploadbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UploadImage();
relativeLayout.setVisibility(View.VISIBLE);
uploadbtn.setVisibility(View.GONE);
}});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// saveData();
dialog.show();
final StorageReference reference = firebaseStorage.getReference().child("Products").child(System.currentTimeMillis() + "");
reference.putFile(ImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot> () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
ProductDetailHelperClass model = new ProductDetailHelperClass();
model.setProductImage(uri.toString());
model.setProductName(ProductName.getText().toString());
model.setProductBrand(ProductBrand.getText().toString());
model.setProductExpiry(ProductExpiry.getText().toString());
model.setProductQty(ProductQty.getText().toString());
model.setProductCalories(ProductCalories.getText().toString());
model.setProductSugar(ProductSugar.getText().toString());
model.setProductFibers(ProductFibers.getText().toString());
model.setProductCarbs(ProductCarbs.getText().toString());
FirebaseDatabase.getInstance().getReference("CompleteData").child("UserProductDetails").setValue(model).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(textActivity.this, "Product Upload Successfully", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} }).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Toast.makeText(textActivity.this, "Error occured", Toast.LENGTH_SHORT).show();
}}); } });} }); } });
ProductExpiry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(textActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
// on below line we are setting date to our edit text.
ProductExpiry.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}},// on below line we are passing year, // month and day for selected date in our date picker.
year, month, day);
datePickerDialog.show();
}}); }
private void UploadImage() {
Dexter.withContext(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,101);
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(textActivity.this,"Permission Denied",Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
} }).check();
}
@Override
protected void onActivityResult(int requestCode, int resulttCode, @Nullable Intent data){
super.onActivityResult(requestCode,resulttCode,data);
if(requestCode==101 && resulttCode ==RESULT_OK){
ImageUri = data.getData();
productImage.setImageURI(ImageUri);
}
}
产品详细信息助手类(助手类)
package com.example.myapplication;
public class ProductDetailHelperClass {
public String ProductName, ProductBrand, ProductExpiry, ProductQty,ProductCalories,ProductSugar,ProductFibers,ProductCarbs;
public String productImage;
public ProductDetailHelperClass() {
}
public ProductDetailHelperClass(String productName, String productBrand, String productExpiry, String productQty, String productCalories, String productSugar, String productFibers, String productCarbs, String productImage) {
ProductName = productName;
this.ProductBrand = productBrand;
ProductExpiry = productExpiry;
ProductQty = productQty;
ProductCalories = productCalories;
ProductSugar = productSugar;
ProductFibers = productFibers;
ProductCarbs = productCarbs;
this.productImage = productImage;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getProductBrand() {
return ProductBrand;
}
public void setProductBrand(String productBrand) {
this.ProductBrand = productBrand;
}
public String getProductExpiry() {
return ProductExpiry;
}
public void setProductExpiry(String productExpiry) {
ProductExpiry = productExpiry;
}
public String getProductQty() {
return ProductQty;
}
public void setProductQty(String productQty) {
ProductQty = productQty;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductCalories() {
return ProductCalories;
}
public void setProductCalories(String productCalories) {
ProductCalories = productCalories;
}
public String getProductSugar() {
return ProductSugar;
}
public void setProductSugar(String productSugar) {
ProductSugar = productSugar;
}
public String getProductFibers() {
return ProductFibers;
}
public void setProductFibers(String productFibers) {
ProductFibers = productFibers;
}
public String getProductCarbs() {
return ProductCarbs;
}
public void setProductCarbs(String productCarbs) {
ProductCarbs = productCarbs;
}}
产品适配器.java
public class ProjectAdapter extends
RecyclerView.Adapter<ProjectAdapter.ViewHolder> {
ArrayList<ProductDetailHelperClass> list;
Context context;
public ProjectAdapter(ArrayList<ProductDetailHelperClass> list,
Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item,
parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int
position) {
ProductDetailHelperClass model = list.get(position);
Picasso.get().load(model.getProductImage()).placeholder(R.drawable.jam2).into(holder.itemImage);
holder.itemHeadLine.setText(model.getProductName());
// holder.itemBrand.setText(model.getProductBrand());
holder.itemQty.setText(model.getProductQty());
holder.itemExpiry.setText(model.getProductExpiry());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context,SingleProductActivity.class);
intent.putExtra("singleImg",model.getProductImage());
intent.putExtra("singleHeadLine",model.getProductName());
intent.putExtra("singleBrand",model.getProductBrand());
intent.putExtra("singleQty",model.getProductQty());
intent.putExtra("singleExpiry",model.getProductExpiry());
intent.putExtra("singleCal",model.getProductCalories());
intent.putExtra("singleSugar",model.getProductSugar());
intent.putExtra("singleFiber",model.getProductFibers());
intent.putExtra("singleCarbs",model.getProductCarbs());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} });}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView itemHeadLine, itemQty, itemBrand, itemExpiry;
ImageView itemImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
itemHeadLine = itemView.findViewById(R.id.itemHeadLine);
itemQty = itemView.findViewById(R.id.itemQty);
// itemBrand= itemView.findViewById(R.id.itemBrand);
itemExpiry = itemView.findViewById(R.id.itemExpiry);
itemImage = itemView.findViewById(R.id.itemImage);
}}}
'
1条答案
按热度按时间wwwo4jvm1#
Firebase SDK基于JavaBean property naming conventions在Java对象和数据库中的JSON属性之间进行Map,这意味着它会查看类中的公共getter/setter和字段。
软件包com.example.myapplication;
同样,根据JavaBean命名约定,第一个公共字段定义了一个属性:
当getter/setter定义属性时:
这是相同属性的两种不同大小写,这也是错误消息所指出的。
最简单的解决方案:
productBrand
而不是ProductBrand
。@PropertyName
注解将它们显式Map到ProductBrand
/这两种方法都不比其他方法好,所以选择你觉得最自然的方法。