【问题标题】:How to retrieve data of a specific user in android studio using firestore如何使用firestore在android studio中检索特定用户的数据
【发布时间】:2021-07-14 06:59:56
【问题描述】:

实际上,我一直在开发一个预约应用程序,用户可以通过选择日期和时间进行预约。我使用 Java 和 Firebase(Firestore) 来设计我的应用程序。 这里我需要检索已登录的特定用户的数据,并将其显示在我的下一个页面(详细信息页面)中。

对于不同的用户,我需要显示的数据应该不同,这意味着如果 A 进入并预约,只有他的详细信息必须可见,而 B 的详细信息不可见。

其实我试过用

db.collection("Documents").document(id).get()

但它会在显示约会详细信息的通知页面中引发错误

请检查我的 Cloud Firestore:

这是完成预订并插入详细信息的图像。 这里是插入id的地方。

Notification.java

public class Notification extends AppCompatActivity {

/* DatabaseReference studentDbRef;
private MyAdapter adapter;
private ArrayList<Booking> list;
*/
private RecyclerView recyclerView;
private FirebaseFirestore db;
private MyAdapter adapter;
private List<Booking> list;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    if (getSupportActionBar() != null)  //remove top actionbar
    {
        getSupportActionBar().hide();
    }
    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(Notification.this));

    db = FirebaseFirestore.getInstance();
    list = new ArrayList<>();
    adapter = new MyAdapter(Notification.this,list);
    recyclerView.setAdapter(adapter);


    showData();

  /*  recyclerView.setAdapter(adapter);
    studentDbRef = FirebaseDatabase.getInstance().getReference("Book");

    studentDbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                Booking book = dataSnapshot.getValue(Booking.class);
                list.add(book);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });*/

}
private void showData(){
    db.collection("Documents").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    list.clear();
                    for(DocumentSnapshot snapshot : task.getResult()){
                        Booking book = new Booking(snapshot.getString("id"),snapshot.getString("date"),snapshot.getString("time"),snapshot.getString("barber"));
                        list.add(book);

                    }
                    adapter.notifyDataSetChanged();
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(Notification.this,"OOPS something went wrong",Toast.LENGTH_SHORT).show();

        }
    });
}

}

当我通过登录进行预约时,我得到了我的数据以及其他用户的数据。想不通,请帮忙。我尝试了其他解决方案,但没有奏效。

【问题讨论】:

  • 你得到什么错误?
  • 我解决了先生,实际上错误是我无法获取已登录的特定用户数据。

标签: android firebase google-cloud-firestore


【解决方案1】:

我想通了:

FirebaseAuth fAuth =FirebaseAuth.getInstance()
String id = fAuth.getCurrentUser().getUid() 

当我检索我使用的数据时

db.collection("Documents").whereEqualTo("uid",id).get() .

这个解决方案确实帮助我获取了已登录的特定用户数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2020-09-24
    • 2018-10-04
    相关资源
    最近更新 更多