如何提供ID以在Cloud Firestore中发布?

时间:2020-11-03 11:31:13

标签: java android firebase google-cloud-firestore

我想在我的帖子的Cloud Firestore中建立此结构: 我想在帖子的Cloud Firestore中构建此结构:

 Users
   |
   |-> UserId
           |
           |-> OneUserPosts
                   |
                   |->PostId
                         |
                         |-> PostContent
                             Comment 1
                             Comment 2

以下是我要创建帖子的活动: 如何给我的帖子提供ID?

  public class Add_Post extends AppCompatActivity {
         EditText posttext;
     FirebaseUser user;
 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__post);
        user = FirebaseAuth.getInstance().getCurrentUser();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        try {
            actionBar.setTitle("Write Your Post");

        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        }catch (NullPointerException e){}
        posttext = findViewById(R.id.PostTextTextMultiLine);
  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.addpostmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.POST:

    savepost();

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void savepost() {
        String Post = posttext.getText().toString();

        if (Post.length()<5){
            Toast.makeText(this, "Please enter at least 10 words ", Toast.LENGTH_SHORT).show();

        }
        else{
         try {

            CollectionReference postref = FirebaseFirestore.getInstance()
                    .collection("Users").document(user.getUid()).collection("OneUserPost");
            postref.add(new posttextlist(Post));
            Toast.makeText(this, "Successfully Posted", Toast.LENGTH_SHORT).show();
            finish();
            startActivity(new Intent(this,MainActivity.class));
}catch (NullPointerException e){}
        }


    }

}

请解决我的问题。我到处搜索,但没有得到答案。 请解决我的问题。我到处搜索,但没有得到答案。

1 个答案:

答案 0 :(得分:2)

使用以下代码行时:

postref.add(new posttextlist(Post));

CollectionReference的add(Object data)

使用指定的数据向此集合添加一个新文档,并自动为其分配一个文档ID。

如果不需要此行为,则应使用DocumentReference的set(Object data)

覆盖此DocumentReference所引用的文档。

所以您的代码应该这样:

CollectionReference postref = FirebaseFirestore.getInstance()
                .collection("Users").document(user.getUid())
                .collection("OneUserPost");
String id = postref.getId(); //Do what you need to do with this id
postref.document(id).set(new posttextlist(Post));