我是Cloud Firestore的新手,目前我正在开发一个android应用程序,这是一个任务管理应用程序。现在我正在开发任务组页面。我可以将任务组名称添加到firebase数据库中,但问题是它会显示给每个登录的用户,所以我希望它显示给添加任务组名称的用户,下面我添加了AddTaskGroup活动的代码,请分享用户添加的任务数据只显示给他的代码。
AddTaskGroup.java
package com.example.todoappwil;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
public class AddTaskGroup extends AppCompatActivity {
private EditText taskGroupNameEdt;
private Button saveTaskGroupBtn;
private FirebaseUser firebaseUser;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private String userID;
private String tskGroupName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_settings);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// final Drawable upArrow = getResources().getDrawable(R.drawable.arrow);
// upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
// getSupportActionBar().setHomeAsUpIndicator(upArrow);
ActionBar actionBar;
actionBar = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#3498DB"));
actionBar.setBackgroundDrawable(colorDrawable);
actionBar.setTitle(Html.fromHtml("<font color='#ffffff'>Add Task Group </font>"));
taskGroupNameEdt = findViewById(R.id.addtskGrpInput);
saveTaskGroupBtn = findViewById(R.id.saveTskGroupBtn);
saveTaskGroupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tskGroupName = taskGroupNameEdt.getText().toString();
if (TextUtils.isEmpty(tskGroupName)) {
taskGroupNameEdt.setError("Enter Task Group");
}
else {
addTaskDatatoFirestore(tskGroupName);
}
}
});
userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
}
private void addTaskDatatoFirestore(String tskGroupName) {
CollectionReference taskGroups =
firebaseFirestore.collection("TaskGroups");
TaskGroupContainer taskGroupContainer = new TaskGroupContainer(tskGroupName);
taskGroups.add(taskGroupContainer).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(AddTaskGroup.this, "Task Group Added Successfully",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddTaskGroup.this, "Fail to add Task Group n" + e,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(AddTaskGroup.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
当您使用以下引用时:
CollectionReference taskGroups =
firebaseFirestore.collection("TaskGroups");
这意味着无论用户是谁,您都试图在TaskGroups
集合下添加文档。如果您需要通过添加文档的人来区分文档,那么您必须为每个对象添加特定的内容,以便进行区分。最简单的解决方案是使用Firebase身份验证并添加用户ID,如下所示:
Firestore-root
|
--- TaskGroups (collection)
|
--- taskId (document)
|
--- tskGroupName: "The best group"
|
--- uid: "veryLongId" 👈
现在,要只获取与用户相对应的文档,例如,uid
等于veryLongId
,请使用以下查询:
Query queryById = taskGroups.whereEqualTo("uid", veryLongId)
但是,您可以在每个用户对象下嵌套一个子任务集合,如下所示:
Firestore-root
|
--- users (collection)
|
--- uid (document)
|
--- TaskGroups (subcollection)
|
--- tskGroupName: "The best group"
不需要将用户的UID存储为字段的情况。要获得用户拥有的所有任务,只需要以下参考:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
CollectionReference taskGroups = firebaseFirestore
.collection("users")
.document(uid)
.collection("TaskGroups");