创建类Details的对象后,当我将其上传到firebase时,它会创建一个具有空值的子对象,同时对象包含一个参数less,即image。 name =""成本="" location =""流派=""电话="" image =""丢失
代码:
package com.example.amitroshan.dairyvilla;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import java.io.ByteArrayOutputStream;
import java.util.Random;
public class Feed extends Fragment {
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("Books");
static final int REQUEST_IMAGE_CAPTURE = 1;
private EditText upload_name, upload_location, upload_cost, upload_genre, upload_phone;
private Button send;
private ImageButton upload;
private String upload_url;
public Feed() {
// Required empty public constructor
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
upload.setImageBitmap(imageBitmap);
//Uri filePath = data.getData();
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://dairyvilla-3623a.appspot.com/");
StorageReference mountainImagesRef = storageRef.child("images/" + new Random(10000) + ".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte[] mdata = baos.toByteArray();
UploadTask uploadTask = mountainImagesRef.putBytes(mdata);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
upload_url = task.getResult().getDownloadUrl().toString();
//pro.dismiss();
}
});
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_feed, container, false);
upload_name = (EditText) v.findViewById(R.id.upload_name);
upload_location = (EditText) v.findViewById(R.id.upload_location);
upload_cost = (EditText) v.findViewById(R.id.upload_cost);
upload_genre = (EditText) v.findViewById(R.id.upload_genre);
upload_phone = (EditText) v.findViewById(R.id.upload_phone);
upload = (ImageButton) v.findViewById(R.id.imageButton);
send = (Button) v.findViewById(R.id.upload_button);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
final Details d = new Details(upload_name.getText().toString(),upload_location.getText().toString(),
upload_cost.getText().toString(),upload_genre.getText().toString(),
upload_url,upload_phone.getText().toString());
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRef.child(mRef.push().getKey()).setValue(d);
Toast.makeText(getContext(), "Uploading Complete..", Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
class Details{
String name, location, cost, genre, phone, image;
public Details(String name, String location, String cost, String genre, String image, String phone) {
this.name = name;
this.location = location;
this.cost = cost;
this.genre = genre;
this.image = image;
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
如果要将对象推送到firebase,则数据模型类需要一个空构造函数。
在这种情况下
public Details() {}
而不是这个
mRef.child(mRef.push().getKey()).setValue(d);
只需使用此
mRef.push().setValue(d);
更新
使用onCreateView-Method中的所有变量创建对象。此时文本视图都是空的,因此它们返回一个空字符串。 尝试移动
final Details d = new Details(upload_name.getText().toString(),upload_location.getText().toString(),
upload_cost.getText().toString(),upload_genre.getText().toString(),
upload_url,upload_phone.getText().toString());
点击监听器内的。
答案 1 :(得分:1)
要在Firebase数据库中推送对象,您需要在类中创建一个空构造函数,然后使用它来推送所有值:
clientName.push().setValue(n);