FirebaseFirestore插入

时间:2018-03-20 21:04:45

标签: android firebase google-cloud-firestore

public class Post extends AppCompatActivity {

private static final String TAG = null;
private TextView titleView;
private Button create_post_button;
private EditText post_title,post_description;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
private FirebaseFirestore db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        mAuth=FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();
        progressBar = (ProgressBar) findViewById(R.id.progressBar2);

        titleView=(TextView) findViewById(R.id.titleView);
        post_title=(EditText) findViewById(R.id.post_title);
        post_description=(EditText) findViewById(R.id.post_description);
        create_post_button=(Button)findViewById(R.id.create_post_button);

        create_post_button.setOnClickListener(new View.OnClickListener()  {

            public void onClick(View v) {
                createPost();
            }
        });
    }

     public void createPost() {
        String title_val = post_title.getText().toString().trim();
        String post_description_val = post_description.getText().toString().trim();
        String user_id=mAuth.getCurrentUser().getUid();
        if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(post_description_val)) {
            progressBar.setVisibility(View.VISIBLE);
            Map<String, String> posts = new HashMap<>();
            posts.put("description",post_description_val);
            posts.put("title", title_val);
            posts.put("user_id", user_id);
            progressBar.setVisibility(View.GONE);
            db.collection("posts")
                    .add(posts)
                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                        @Override
                        public void onSuccess(DocumentReference documentReference) {
                            Toast.makeText( Post.this,"Success", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.w( TAG,"Error adding document", e);
                        }});}}}

它没有被插入到db中,也没有给出任何错误。我不明白错误在哪里!我不知道出了什么问题,因为一切都来自 android 主页。如果这是一些逻辑错误请告诉我! 一些帮助将不胜感激!提前致谢!

1 个答案:

答案 0 :(得分:1)

问题是因为您在检查空标题和说明时错误地访问了变量,这里:

if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(description_val)) {

它应该是:

public void createPost() {
    String title_val = title.getText().toString().trim();
    String post_description_val = description.getText().toString().trim();
    String user_id=mAuth.getCurrentUser().getUid();
    if (!title_val.isEmpty() && !post_description_val.isEmpty()) {
        progressBar.setVisibility(View.VISIBLE);
        Map<String, String> posts = new HashMap<>();
        posts.put("description",description_val);
        posts.put("title", title);
        posts.put("user_id", user_id);
        progressBar.setVisibility(View.GONE);
        db.collection("posts")
                .add(posts)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        Toast.makeText( Post.this,"Success", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w( TAG,"Error adding document", e);
                    }
                });
    }

}

修改

出于测试目的,您的规则应如下所示,稍后您需要更新它们以使其更安全: enter image description here