【问题标题】:FirebaseFirestore insertionFirebaseFirestore 插入
【发布时间】:2018-03-20 21:04:44
【问题描述】:
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);
                        }});}}}

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

【问题讨论】:

  • 由于您使用的是 Map 而不是 POJO,您是否尝试过使用 db.collection("posts").document().set(posts)...?
  • @HondaGuy 不,我没试过,请你写完整的说明..
  • @HondaGuy 我试过设置同样的问题
  • 刚刚测试了你的代码,查看答案
  • @LeviAlbuquerque 看看上面的代码,还是不行,有什么问题

标签: android firebase google-cloud-firestore


【解决方案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);
                    }
                });
    }

}

编辑:

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

【讨论】:

  • Levi 我更新了问题,你可以看看完整的代码。它没有解决我的问题
  • 好的,我已经运行了你的代码,它对我来说很好用。我有一些问题: 1 当您到达 Post 活动时,您是否已通过身份验证? 2 Firestore 是否在您的 Firebase 控制台中激活? 3 你的Firestore规则允许写吗? 4 你能用简单的 title_val.isEmpty() 和 position_description_val.isEmpty() 替换你的 textutils 方法吗?
  • 1.是的 2.我认为所有依赖项都可以 3.我在哪里看到这个?4.我尝试删除!,代码不运行
  • 我已更新代码以使其适用于问题 #4。我还添加了您可以看到 #3 的位置
  • Lev 你能帮我解决其他问题吗
猜你喜欢
  • 1970-01-01
  • 2022-12-02
  • 2021-02-24
  • 2019-02-20
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
相关资源
最近更新 更多