1

I want to retrieve data from firebase in expandableListView. In header, I want 4 value like table no, invoice no, date, bill amount. Help me to find out the solution with searching using invoice no and date, when clicking on item expand the item and display item info.

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

}

KENdi
  • 7,576
  • 2
  • 16
  • 31
np5293
  • 7
  • 2

1 Answers1

0

Because of the asynchronously behaviour, you need to move the declaration of the billarry ArrayList inside the onDataChange() method.

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

So remember, onDataChange is always called asynchronously. This means that the statement that adds objects of BillModel class to the list is executed before onDataChange has been called. That's why your list is empty outside that method.

For other approach, please visit this post and this post.

Hope ot helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193