【问题标题】:How to retrieve specific variable in firebase with ListView?如何使用 ListView 检索 firebase 中的特定变量?
【发布时间】:2017-01-16 11:01:14
【问题描述】:

使用 Firebase ListAdapter,我需要从多个节点检索数据并检索具有条件的特定变量

例子:

+-------------+--------------+
|   node A    |    node B    |
+-------------+--------------+
| name: jon   | name: leen   |
| age : 33    | age:  33     |
| city: amman | city: jeddah |
+-------------+--------------+

我需要检索数据select name from npdeA, nodeB where name = "leen"。我该怎么做?

这是我的代码

enter code here // Real Code Start Here
    ListView listView = (ListView) View.findViewById(R.id.MyList);
    final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://assss-bf843.firebaseio.com/users");


    final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(getActivity(),String.class,android.R.layout.simple_list_item_2,databaseReference) {



        protected void populateView(View v, String model, int position) {

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

            textView.setText(model);


        }


    };                                                                   //////////getActivity(), ApContent.class, R.layout.layout_action_plan, ref

    listView.setAdapter(firebaseListAdapter);

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

    您首先创建一个自定义类,例如:

    class UserDetails {
        private String name, city;
        private int age;
    
        public userDetails() {
        }
    
        public userDetails(String name, String city, int age) {
            this.name = name;
            this.city = city;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    Q)为什么需要创建自定义类?

    A)因为您必须获取不同类型的多条记录。

    现在在 FirebaseListAdapter 构造函数中将第二个参数作为 UserDetails.class(您的自定义类)传递。

    现在 Firebase 将获取完整的详细信息,作为您的自定义类 (UserDetails) 中提及的内容。

    现在在您的 populateView 方法中,您可以检查名称的值是否等于“leen”。如果是,那么您可以将其设置到您的文本视图中。

    所以像这样更新你的 populateView:

    protected void populateView(View v, UserDetails model, int position) {
    
        if (model.getName().equals("leen")) {
            TextView textView = (TextView) v.findViewById(android.R.id.text1);
    
            textView.setText(model);
        }
    
    }
    

    现在你将实现你想要的。

    顺便说一句,更好的方法是创建自定义 ViewHolder 类。

    更新

    我认为您没有在 FirebaseRecyclerAdapter 类名之后的尖括号内写入正确的占位符。所以只需复制我的代码即可:

    FirebaseRecyclerAdapter<UserDetails, View> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserDetails, View>(UserDetails.class, android.R.layout.simple_list_item_2, View.class, databaseReference) {
                @Override
                protected void populateViewHolder(View v, UserDetails model, int position) {
    
                if (model.getName().equals("leen")) {
                    TextView textView = (TextView) v.findViewById(android.R.id.text1);
    
                    textView.setText(model);
                }
            }
        };
    
            listView.setAdapter(firebaseRecyclerAdapter);
    

    【讨论】:

    • 非常感谢,几小时后我返回结果
    • Error:(40, 179) 错误: 不是抽象的,不会覆盖 FirebaseListAdapter 中的抽象方法 populateView(View,String,int) 什么问题?
    • 确保您在new FirebaseListAdapter(...) 中正确覆盖populateView(...) 方法
    • 我确定 ... FirebaseListAdapter 中的 populateView() .. 请帮助我
    • @SaifAlbashiti 更新了我的答案,现在这将解决您的问题。顺便说一句,我建议你先学习一下java,然后再做android。
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 2023-03-27
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多