FirebaseListAdapter按字母顺序排列

时间:2017-01-05 18:02:08

标签: android firebase firebase-realtime-database firebaseui

在我的Android应用中,我有一个带有FirebaseListAdapter的ListView向我显示我的用户,现在我希望firebaseListAdapter按字母顺序排序。我怎样才能做到这一点?

代码:

mListView = (ListView) findViewById(R.id.listView);

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
                        this,
                        String.class,
                        android.R.layout.simple_list_item_1,
                        databaseReference
                ){
                    @Override
                    protected void populateView(View v, String model, int position) {

                        TextView textView = (TextView) v.findViewById(android.R.id.text1);
                        textView.setText(model);

                    }
                };

mListView.setAdapter(firebaseListAdapter);

2 个答案:

答案 0 :(得分:1)

这取决于名称是什么。如果您将用户存储在其名称下(以便名称是密钥):

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
    this,
    String.class,
    android.R.layout.simple_list_item_1,
    databaseReference.orderByKey()
){

但是,例如,如果您将名称作为名为“name”的属性,则使用:

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
    this,
    String.class,
    android.R.layout.simple_list_item_1,
    databaseReference.orderByChild("name")
){

最后:如果您想按键的单个字符串值进行排序:

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
    this,
    String.class,
    android.R.layout.simple_list_item_1,
    databaseReference.orderByValue()
){

答案 1 :(得分:0)

我找到了解决方案!

以下是工作代码:

mListView = (ListView) findViewById(R.id.listView);

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
                        this,
                        String.class,
                        android.R.layout.simple_list_item_1,
                        databaseReference.orderByValue()
                ){
                    @Override
                    protected void populateView(View v, String model, int position) {

                        TextView textView = (TextView) v.findViewById(android.R.id.text1);
                        textView.setText(model);

                    }
                };

mListView.setAdapter(firebaseListAdapter);