简体   繁体   English

Android - 对recyclerview的多个查询(Firebase)

[英]Android - Multiple queries for recyclerview (Firebase)

I am working with recylerviews and Firebase. 我正在使用recylerviews和Firebase。 I am using FirebaseUI to populate data to my recyclerview. 我正在使用FirebaseUI将数据填充到我的recyclerview。 I was wondering if it was possible to use two queries within my fragment. 我想知道是否可以在我的片段中使用两个查询。 The query that should be executed should be dependent of if a node in the database exists. 应该执行的查询应该取决于数据库中是否存在节点。

Database Structure: 数据库结构:

在此输入图像描述

If the address child is present in the users node, my fragment should query the users node. 如果地址子节点存在于users节点中,则我的片段应查询users节点。 If not it should query the routes node. 如果不是,它应该查询路由节点。 Is this possible? 这可能吗?

Basically here I am making by query which gets me all markers: 基本上我在这里通过查询来获取所有标记:

Query keyQuery = FirebaseDatabase.getInstance().getReference(sharedPreferences.getString("school", null)).child("markers");
    recyclerView = (RecyclerView) rootView.findViewById(R.id.markerRecyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    FirebaseRecyclerOptions<FirebaseMarker> options = new FirebaseRecyclerOptions.Builder<FirebaseMarker>()
            .setQuery(keyQuery, FirebaseMarker.class)
            .build();

Inside my onBindViewHolder method I have an onClick on each item in the recyclerview. 在我的onBindViewHolder方法中,我在recyclerview中的每个项目上都有一个onClick。 When the item is clicked, the user goes to a new activity. 单击该项目后,用户将转到新活动。 In this new activity the user can press a button which will add the address node under the users/userId node. 在这个新活动中,用户可以按下一个按钮,该按钮将在users / userId节点下添加地址节点。 What pressing that button means is that the user have chosen that marker. 按下该按钮意味着用户已选择该标记。 So I only want to show that marker information in the recyclerview and not every marker in the database. 所以我只想在recyclerview中显示标记信息,而不是在数据库中显示每个标记。

 holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(),MainActivity.class);
                    intent.putExtra("databaseKey", key);
                    startActivity(intent);
                }
            });

I was thinking that: If a address and time node was inserted in the users node, I could just query this, which would make it easier. 我在想:如果在用户节点中插入了地址时间节点,我可以查询它,这样可以更容易。

This is possible, although I think you'd first need to check whether the user has selected a marker and then decide which query to attach to the FirebaseRecyclerAdapter . 这是可能的,但我认为您首先需要检查用户是否选择了标记,然后决定将哪个查询附加到FirebaseRecyclerAdapter

To do that, it would likely be necessary to add a list of users that have selected each marker under the marker nodes, something like: 为此,可能需要添加已在标记节点下选择每个标记的用户列表,例如:

users
  userId
    selectedMarker      // the ID of the marker selected by this user
    ...
markers
  markerId
    ...
    selectedUsers       // list of user IDs that have selected this marker

Then, if the users node contains a selectedMarker value, you could use the below query to get all markers selected by this specific user: 然后,如果users节点包含selectedMarker值,您可以使用以下查询来获取此特定用户选择的所有标记:

Query query = FirebaseDatabase.getInstance().getReference()
        .child(schoolId).child("markers")
        .orderByChild("selectedUsers/"+userId).equalTo(true);

Where schoolId is from your sharedPreferences.getString("school", null) and userId is the currently logged in user's unique ID. 其中schoolId来自您的sharedPreferences.getString("school", null)userId是当前登录用户的唯一ID。

To check if the user has selected a marker, could be as simple as: 要检查用户是否选择了标记,可以像下面这样简单:

FirebaseDatabase.getInstance().getReference()
        .child(schoolId).child("users").child(userId)
        .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child("selectedMarker").exists()) {
                    // Attach above query to FirebaseRecyclerAdapter
                } else {
                    // Attach markers reference (no query) to FirebaseRecyclerAdapter
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

However, if you just want to display the user's single selected marker, it's likely that you don't need to use the RecyclerView at all, and could just use a separate view to display details about the user's selected marker. 但是,如果您只想显示用户的单个选定标记,则可能根本不需要使用RecyclerView ,并且可以使用单独的视图来显示有关用户所选标记的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM