简体   繁体   English

如何通过Firebase数据库填充ExpandableListView子视图?

[英]How can I populate an ExpandableListView child views through Firebase database?

I want to populate my expandable list view with the values from Firebase database. 我想用Firebase数据库中的值填充可扩展列表视图。 I am able to add data to views statically but through Firebase not. 我能够将数据静态添加到视图中,但不能通过Firebase添加。

This is my code for that: 这是我的代码:

public class ExpandableListitems {
public List<String> food;

public HashMap<String, List<String>> getData() {
    final HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();



    //getting fooditems from database
    FirebaseDatabase firebaseDatabasefighter = FirebaseDatabase.getInstance();
    DatabaseReference databaseReferencefighter = firebaseDatabasefighter.getReference("food").child("food_details");
    ValueEventListener valueEventListenerfighter = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                food = new ArrayList<String>();
                String value = String.valueOf(ds.getValue());


                food.add(value);

                Log.i("vipan",value);
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    databaseReferencefighter.addValueEventListener(valueEventListenerfighter);


    food.add("milk");
    food.add("dahi");
    food.add("paratha");
    food.add("aaloo");


   expandableListDetail.put("FoodItems", food);
    return expandableListDetail;

And my database looks like this: 我的数据库如下所示:

  food

food_details: "2% Fat Milk" food_details:“ 2%脂肪牛奶”

i want to add this food_details value into the listview's view but unable so far.How can i implement this? 我想将此food_details值添加到listview的视图中,但到目前为止无法执行。我该如何实现呢?

You cannot return something now that hasn't been loaded yet. 您现在无法返回尚未加载的内容。 With other words, you cannot simply return the expandableListDetail HashMap<String, List<String>> which contains a list of strings that is coming from the database as a result of a method because this list will always be empty due the asynchronous behaviour of this method. 换句话说,您不能简单地返回expandableListDetail HashMap<String, List<String>> ,其中包含作为方法的结果来自数据库的字符串列表,因为由于该列表的异步行为,该列表将始终为empty方法。 This means that by the time you are trying to return that result, the data hasn't finished loading yet from the database and that's why is not accessible. 这意味着,当您尝试返回该结果时,数据尚未从数据库中完成加载,因此无法访问。

A quick solve for this problem would be to use the food list only inside the onDataChange() method, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. 一个快速的解决这个问题是使用的food里面只有列表onDataChange()方法,否则我建议你看我anwser从这个最后部分职位中,我已经解释了如何可以使用自定义的回调来完成。 You can also take a look at this video for a better understanding. 您也可以观看此视频 ,以更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用Firebase数据库填充微调器? - How can I populate a spinner with my Firebase database? 如何更改 Firebase 数据库中子项的值? - How can I change the value of child in Firebase database? 如何处理ExpandableListView子行的行中不同视图的事件单击 - How to handle events clicks of different views within a row of a ExpandableListView child 如何用Firebase数据填充Spinner? - How can I populate a Spinner with Firebase data? 适用于 Android 的 Firebase,我如何循环遍历一个孩子(对于每个孩子 = x 做 y) - Firebase for Android, How can I loop through a child (for each child = x do y) 如何使用 Firebase 实时数据库中的关联节点名称列表填充“微调器”? - How can I populate the 'Spinner' with a list of associated node names from the Firebase Realtime Database? 无法通过Servlet填充数据库 - Can not populate database through servlet 如何阻止从 Firebase 数据库填充并由 FirebaseRecyclerAdapter 管理的 RecyclerView 重新创建已删除的视图? - How can I stop my RecyclerView, populated from Firebase Database and managed by FirebaseRecyclerAdapter, from recreating deleted views? 在ExpandableListView中,如何在子结果的末尾显示另一行? - In an ExpandableListView, how can I show one additional line at the end of the child results? 在expandableListView android中添加动态子视图 - Add dynamic child views in expandableListView android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM