我无法在消耗型列表视图中检索firebase数据

时间:2017-06-06 11:31:27

标签: android firebase firebase-realtime-database firebase-authentication expandablelistview

我想在expandableListView中从firebase检索数据。在标题中,我想要4个值,如表格no,发票号,日期,账单金额。帮我找出解决方案 使用发票号和日期​​进行搜索,点击项目时展开项目并显示项目信息。

public class MainActivity extends ActionBarActivity {
private static ExpandableListView expandableListView;
private static ExpandableListAdapter adapter;
private DatabaseReference mRef;
private FirebaseDatabase mFirebaseInstance;
BillModel bill;




 ArrayList<BillModel> billarry = new ArrayList<BillModel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expandableListView = (ExpandableListView) findViewById(R.id.simple_expandable_listview);
    mFirebaseInstance = FirebaseDatabase.getInstance();
    // Setting group indicator null for custom indicator
    expandableListView.setGroupIndicator(null);

    setItems();
    setListener();

}
 void setItems() {

    mRef = mFirebaseInstance.getReference();
    DatabaseReference queryrecord = mRef.child("bill");
    queryrecord.addValueEventListener(new ValueEventListener() {
        public void onDataChange(DataSnapshot snapshot) {
            billarry.clear();
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                bill = postSnapshot.getValue(BillModel.class);
               String bill_date = bill.getBill_date();
               String Bill_amount = bill.getBill_amount();
               String Bill_tableno = bill.getBill_tableno();
               String Incvoice_no = bill.getIncvoice_no();
                Log.d("result", "Bill---" + bill_date+","+Bill_amount+","+Bill_tableno+","+Incvoice_no);
                billarry.add(bill);
                // here you can access to name property like university.name

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

        }


    });
 // Array list for header
    ArrayList<String> header = new ArrayList<String>();

    // Array list for child items
    List<String> child1 = new ArrayList<String>();
    List<String> child2 = new ArrayList<String>();
    List<String> child3 = new ArrayList<String>();
    List<String> child4 = new ArrayList<String>();

    // Hash map for both header and child
    HashMap<ArrayList<BillModel>, List<String>> hashMap = new HashMap<>();


    // Adding header and childs to hash map
    hashMap.put(billarry, child1);
Log.d("result", "HAsh-Bill---" +  hashMap.put(billarry,child1 ));


    hashMap.put(billmodel.get(1), child2);
    hashMap.put(billmodel.get(2), child3);
    hashMap.put(billmodel.get(3), child4);

    adapter = new ExpandableListAdapter(MainActivity.this, header, hashMap);

    // Setting adpater over expandablelistview
    expandableListView.setAdapter(adapter);
}
void setListener() {

    // This listener will show toast on group click
    expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView listview, View view,
                                    int group_pos, long id) {

            Toast.makeText(MainActivity.this,
                    "You clicked : " + adapter.getGroup(group_pos),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    });
expandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {

                // Default position
                int previousGroup = -1;

                @Override
                public void onGroupExpand(int groupPosition) {
                    if (groupPosition != previousGroup)

                        // Collapse the expanded group
                        expandableListView.collapseGroup(previousGroup);
                    previousGroup = groupPosition;
                }

            });
 // This listener will show toast on child click
    expandableListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView listview, View view,
                                    int groupPos, int childPos, long id) {
            Toast.makeText(
                    MainActivity.this,
                    "You clicked : " + adapter.getChild(groupPos, childPos),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

由于异步行为,您需要在billarry方法中移动onDataChange() ArrayList的声明。

ArrayList<BillModel> billarry = new ArrayList<BillModel>();

所以请记住,onDataChange始终是异步调用的。这意味着在BillModel被调用之前执行将onDataChange类的对象添加到列表的语句。这就是为什么你的列表在该方法之外是空的。

对于其他方法,请访问此post和此post

希望不会有帮助。