来自 Firestore 的数据未显示

时间:2021-05-16 16:57:01

标签: java database firebase android-studio google-cloud-firestore

这是当前我的 FireStore 数据库的结构(可以更改):enter image description here 对于给定用户(当前用户),我想显示他们的“匹配项”(其他用户的 ID、姓名和图片) 这就是我正在尝试的:

public class MatchesActivity extends AppCompatActivity {
    transient RecyclerView mRecyclerView;
    transient RecyclerView.Adapter mMatchesAdapter;
    transient RecyclerView.LayoutManager mMatchesLayoutManager;
    transient String currentUserID;
    transient FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matches);
        currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setNestedScrollingEnabled(false);
        mMatchesLayoutManager = new LinearLayoutManager(MatchesActivity.this);
        mRecyclerView.setLayoutManager(mMatchesLayoutManager);
        mMatchesAdapter = new MatchesAdapter(getDataSetMatches(), MatchesActivity.this);
        mRecyclerView.setAdapter(mMatchesAdapter);
        firestore = FirebaseFirestore.getInstance();
        getUserMatchId();
    }

    private void getUserMatchId() {
        firestore.collection("users").document(currentUserID).collection("connections").get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            FetchMatchInformation(document.getId());
                        }
                    }
                });
    }
    private void FetchMatchInformation(String Key) {
        firestore.collection("users").document(Key).get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        String id = task.getResult().getId();
                        String name = "";
                        name = task.getResult().getString("name");
                        String picture = "default";
                        picture = task.getResult().getString("profileImageUrl");
                        MatchesObject obj = new MatchesObject(id, name, picture);
                        resultsMatches.add(obj);
                        mMatchesAdapter.notifyDataSetChanged();
                    }
                });
    }
        private ArrayList<MatchesObject> resultsMatches = new ArrayList<MatchesObject>();
        private List<MatchesObject> getDataSetMatches() {
            return resultsMatches;
        }
    }

我很确定我没有从 FIRESTORE 正确获取信息,因此没有将任何内容添加到匹配列表中,也没有显示任何内容。我该如何解决这个问题?

0 个答案:

没有答案