【问题标题】:Retrieving data from a list of uid's从 uid 列表中检索数据
【发布时间】:2015-12-08 18:15:33
【问题描述】:

我正在研究一些 Android/Firebase 的东西,但遇到了问题,似乎无法在线找到解决方案;这就是为什么我在这里希望得到一些帮助。我正在尝试使用具有以下数据库结构的消息节点检索数据:

-root -messages -uid(unique user ID) -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -uid(unique user ID) -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -uid(unique user ID) -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -unique message id(via push) -description: a brief description about this first message. -title: the first title -users -uid(unique user ID) -active: true -fname: Robert -lname: Smith -email: test1@gmail.com -uid(unique user ID) -active: true -fname: Douglas -lname: Crockford -email: test2@firebase.com -uid(unique user ID) -active: true -fname: John -lname: Doe -email: test3@firebase.com

这是我的类 MyListingsActivity 扩展 ListActivity 的简要外观:

Firebase.setAndroidContext(this);
    mFirebaseRef = new Firebase("firebase-url/posts");

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    adapter = new FirebaseListAdapter<Posts>(this, Posts.class, android.R.layout.two_line_list_item, mFirebaseRef.child("uid - unique user ID")){

        @Override
        protected void populateView(View v, Posts model) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(model.getTitle());
            ((TextView)v.findViewById(android.R.id.text2)).setText(model.getDescription());
        }
    };
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String postTitle = ((TextView) v.findViewById(android.R.id.text1)).getText().toString();
    String postDesc = ((TextView) v.findViewById(android.R.id.text2)).getText().toString();

    Intent i = new Intent(getApplicationContext(), SingleListView.class);
    i.putExtra("postTitle", postTitle);
    i.putExtra("postDescription", postDesc);
    startActivity(i);

}

每条消息对应一个特定的现有用户。

我想检索消息中的所有数据并从中创建一个列表视图。我目前能够使用 uid 检索特定用户的数据,但不能检索每个用户的数据。

这是我的代码和 Firebaseadapter

mFirebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                Posts post = postSnapshot.getValue(Posts.class);
                System.out.println(post.getTitle() + " - " + post.getDescription());
            }
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

    adapter = new FirebaseListAdapter<Posts>(this, Posts.class, android.R.layout.two_line_list_item, mFirebaseRef.child(user id)){
        @Override
        protected void populateView(View v, Posts model) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(model.getTitle());
            ((TextView)v.findViewById(android.R.id.text2)).setText(model.getDescription());
        }
    };
    setListAdapter(adapter);

谢谢!

【问题讨论】:

  • 读取整个节点的一种方法是使用 addValueEventListener - 它读取数据库节点的全部内容。在这种情况下,您想要检索消息中的所有数据(一次?)。它将填充一个快照(使用 onDataChange),您可以对其进行迭代以创建列表视图。你还需要什么吗?哦 - 读取节点中的所有内容可能会很棘手,具体取决于它有多少数据。 100 万个项目可能会超出内存存储。
  • 您好杰,感谢您的回复。我正在使用 firebaseadapter,并且一直难以检索数据并将其添加到适配器中。我添加了一些代码来说明我正在尝试做的事情。

标签: java android firebase firebase-realtime-database


【解决方案1】:

您必须使用 addValueEventListener 直接加载您的消息数据,但不使用用户的 UID。例如,如果您使用"messages/{uid}" 格式来获取mFirebaseRef,则应该只使用"messages"

这样,消息下的所有内容都将加载到您的快照中。现在您还不能将其转换为正确的模型或 POJO,因为它不是您的数据模型(Post)的确切格式,但它是一个 Map。因此,您必须对其进行迭代以获取数据模型。类似于以下代码。

DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReference("messages");
mFirebaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map<String, Map> map = (Map<String, Map>) dataSnapshot.getValue();
        mMessages.clear();
        if (map != null) {
        for (String key : map.keySet()) {//keys will be uid
            Map<String, Map> map2 = map.get(key);
            for (String key2 : map2.keySet()) {
                Map<String, Map> map3 = map2.get(key2); //this map will be your Data Model
                mMessages.add(new Posts(map3)); //add a constructor in the Posts class to get values from the map3 and set the properties or you can convert it into Gson and generate your data model directly from the gson string.
            }
        }
    }
}

PS:不要怪我复杂的代码,你的数据库结构负责;-)
PPS:可能有更优雅或优化的方式来做到这一点,但我只会这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2015-10-01
    • 1970-01-01
    • 2020-03-14
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多