【问题标题】:List users with specific child value in a ListView Firebase Database在 ListView Firebase 数据库中列出具有特定子值的用户
【发布时间】:2018-09-12 19:36:49
【问题描述】:

我正在尝试将 Firebase 实时数据库中子 session: 2011 的所有用户列出到 ListView 中。这是我的数据结构:

{
  "Users" : {
    "uid1" : {
      "name" : "Jhon",
      "session" : 2011
    },
    "uid2" : {
      "name" : "Kim",
      "session" : 2011
    }
  }
}

这是我的列表活动:

public class ListActivity extends AppCompatActivity {

    ListView lv;
    FirebaseListAdapter adapter;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUser();
        String userID = user.getUid();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
        ref.child(userID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String session = dataSnapshot.child("session").getValue().toString();
                if (session.equals("2011")) {
                    Query query = FirebaseDatabase.getInstance().getReference().child("Users");

                    lv = (ListView) findViewById(R.id.listView);
                    FirebaseListOptions < profile > options = new FirebaseListOptions.Builder < profile > ()
                        .setLayout(R.layout.profile)
                        .setQuery(query, profile.class)
                        .build();
                    adapter = new FirebaseListAdapter(options) {
                        @Override
                        protected void populateView(View v, Object model, int position) {
                            TextView name = v.findViewById(R.id.name);
                            TextView session = v.findViewById(R.id.session);

                            profile user = (profile) model;
                            name.setText("Name: " + user.getName().toString());
                            session.setText("Session: " + user.getSession().toString());
                        }
                    };
                    lv.setAdapter(adapter);
                } else {
                    //
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

这是我的profile 课程:

public class profile {
    private String name;
    private String session;

    public profile() {}

    public profile(String name, String session) {
        this.name = name;
        this.session = session;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSession() {
        return session;
    }

    public void setSession(String session) {
        this.session = session;
    }

}

但问题是,它没有返回任何东西。我在这里想念什么?

作为参考,我尝试了以下,但是当我这样做时,它只返回了当前登录的用户的值。没有别的。

Query query = FirebaseDatabase.getInstance().getReference().child("Users");

lv = (ListView) findViewById(R.id.listView);
FirebaseListOptions < profile > options = new FirebaseListOptions.Builder < profile > ()
    .setLayout(R.layout.profile)
    .setLifecycleOwner(IdCheckActivity.this)
    .setQuery(query, profile.class)
    .build();
adapter = new FirebaseListAdapter(options) {
    @Override
    protected void populateView(View v, Object model, int position) {
        TextView name = v.findViewById(R.id.name);
        TextView session = v.findViewById(R.id.session);

        profile user = (profile) model;
        name.setText("Name: " + user.getName().toString());
        session.setText("Session: " + user.getSession().toString());
    }
};
lv.setAdapter(adapter);
}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.startListening();
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    您可以很好地使用DatabaseReference,而不是使用Query,并且在orderByChild() 的帮助下,可以这样做:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    
    
    
    rootRef.child("Users").orderByChild("session").equalTo(2011).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot data: dataSnapshot.getChildren()){
                        String tempName = data.child("name").getValue(String.class);
    
                       //with tempName you can access their usernames and you will only get the usernames with session 2011, so you can directly populate your listView from here and use it
    
                    }
    
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
    

    tempName 变量只会获取Users 子级的name,其中session 为2011。此外,如果2011 存储为String,则像“2011”一样使用它。

    编辑:另外,如果您没有收听更改,只是想看看是否存在这样的User,那么您可以在valueEventListener() 中使用if(dataSnapshot.exists())

    使用此代码填充listView 非常简单,与您所做的非常相似,看起来像这样:

    ArrayList array = new ArrayList<String>;
    ListView listView;
    
    //set the findViewById for the listView
    
    //add the following code in the valueEventListener
    
    array.add(tempName);
    
    ArrayAdapter adapter = new ArrayAdapter(YourActivity.this, android.R.layout.simple_list_item_1, array);
                    listView.setAdapter(adapter);
    

    【讨论】:

    • 哇!非常感谢您的回答。如果您可以使用代码更新答案以填充此示例的列表视图,将会非常有帮助。
    猜你喜欢
    • 2017-06-18
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多