【问题标题】:Firebase UI - How to synchronize a ListView with complex dataFirebase UI - 如何将 ListView 与复杂数据同步
【发布时间】:2016-08-07 02:12:22
【问题描述】:

我正在为 Android 编写一个应用程序,其中有事件和用户。

一个事件有很多用户,一个用户有很多事件。

所以我这样构建我的数据库:

- events
    - <event_id>
        - ...
        - users
            - <user_id1>
            - <user_id2>
            - ...
- users
   - <user_id1>
        - <user_detail1>
        - <user_detail2>
        - ...
   - <user_id2>
        - <user_detail1>
        - <user_detail2>
        - ...

现在,我有一个带有 ListView 的 Activity,并且我成功地使用 FirebaseListAdapter 显示了带有 FirebaseUI 的所有事件的列表。

我有第二个 Activity,我想在其中显示事件详细信息,另一个 ListView 显示该事件的用户列表。

我的问题是:如果我使用 FirebaseListAdapter,我只能显示 user_id,如何检索用户详细信息并在 ListView 中显示?

编辑

更准确地说,我希望将特定事件的用户列表链接到 ListView。我想为此使用 FirebaseListAdapter,所以我的代码现在看起来像这样:

fireAdapter = new FirebaseListAdapter<String>(
                this,
                String.class,
                android.R.layout.simple_list_item_1,
                mRootRef.child("events").child("users") //here I have all user_id that I want to retrieve details for
        ) {
            @Override
            protected void populateView(View v, String model, int position) {
                //the string "model" will now have a user_id
                TextView userName = (TextView)v.findViewById(android.R.id.text1); //I want to populate this field

                String usernameString;

                //How do I get to retrieve the username from user_id into this variable?

                userName.setText(usernameString);
            }
        };

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

    要获取用户详细信息,您可以将侦听器附加到用户 ID。侦听器将自动调用onChildAdded()onChildChanged() 方法。对于您的情况,由于您只需要第一次的信息,您可以使用 dataSnapshot.getChildren() 方法在函数中返回的快照检索所有值,并使用函数 dataSnapShot.child('blah-blah').value() 获取确切值

    如需完整参考,请查看Link

    您的代码可能看起来像这样

    private DatabaseReference mDatabase;
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child('users').child('user_id1').setChildListener(ListenerName);
    

    【讨论】:

    • 好的,感谢您的回复,我发现可以在 FirebaseListAdapter 的 poplateView 函数中附加一个监听器,并且工作起来就像一个魅力 :) 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2016-09-18
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多