从uid列表中检索数据

时间:2015-12-08 18:15:34

标签: java android firebase firebase-realtime-database

我正在涉及一些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

这里扩展ListActivity的类MyListingsActivity看起来很简单:

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);

谢谢!

1 个答案:

答案 0 :(得分:1)

You'll have to load your messages data directly using addValueEventListener, but without using the UID of the users. For example, if you were using the format "messages/{uid}" to get mFirebaseRef, you should just use "messages".

This way, all the content under messages will get loaded in your snapshot. Now you won't be able to cast this into a proper model or POJO yet, since it's not the exact format of your data model (Post),but it's a Map. So you'll have to iterate it to get your data model. Something like the following code.

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: Don't blame me for the complex code, your database structure is responsible for that ;-)
PPS: There could be a more elegant or optimized way to do that, however I would have done it this way only.