我无法在消耗性listView中检索firebase数据



我想从ExpanablelistView中的firebase检索数据。在标题中,我想要4个值,例如表号,发票号,日期,账单金额。帮助我找出解决方案通过使用发票号和日期进行搜索,单击项目扩展项目并显示项目信息。

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;
        }
    });
}

}

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

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

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

有关其他方法,请访问此帖子和此帖子。

希望ot会有所帮助。

最新更新