gpt4 book ai didi

android - FirebaseFirestore 插入

转载 作者:行者123 更新时间:2023-11-30 00:03:51 24 4
gpt4 key购买 nike

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 主页。如果这是一些逻辑错误,请让我知道!一些帮助将不胜感激!提前致谢!

最佳答案

问题是因为您在检查空标题和描述时访问了错误的变量,此处:

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

关于android - FirebaseFirestore 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com