2

how can i use firebase to retrieve data for my Expandable listview. my firebase node is like this..

Firebase Node

Adapter class:

public class CustomExpandableListViewAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> headerItem;
private HashMap<String, List<String>> childItem;


public CustomExpandableListViewAdapter(Context context, List<String> headerItem, HashMap<String, List<String>> childItem) {
    this.context = context;
    this.headerItem = headerItem;
    this.childItem = childItem;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return childItem.get(headerItem.get(groupPosition)).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    String childText = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_items, null);
    }
    TextView tv = convertView.findViewById(R.id.tvChildItem);
    tv.setText(childText);

    return convertView;

}

@Override
public int getChildrenCount(int groupPosition) {
    return childItem.get(headerItem.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return headerItem.get(groupPosition);
}

@Override
public int getGroupCount() {
    return headerItem.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_items, null);
    }
    TextView tv = convertView.findViewById(R.id.tvItemHeader);
    tv.setText(headerTitle);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

MainActivity :

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;
    CustomExpandableListViewAdapter customExpandableListViewAdapter;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    FirebaseDatabase database;
    DatabaseReference myRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Expandable ListView Data");

        expandableListView = findViewById(R.id.expLv);
        SetStandardGroups();
        customExpandableListViewAdapter = new CustomExpandableListViewAdapter(this, listDataHeader, listDataChild);
        expandableListView.setAdapter(customExpandableListViewAdapter);
    }

    public void SetStandardGroups() {

        listDataHeader = new ArrayList<>();
        listDataChild = new HashMap<>();

        myRef.addChildEventListener(new ChildEventListener() {
            int counter = 0;
            List<String> childItem = new ArrayList<>();

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                final String headerTitle = dataSnapshot.getKey();
                listDataHeader.add(headerTitle);
                Log.e("TAG", headerTitle);

                for (DataSnapshot ds : dataSnapshot.getChildren()){
                    String child = (String) ds.getValue();
                    childItem.add(child);
                }

                listDataChild.put(listDataHeader.get(counter), childItem);
                counter++;
                Log.e("TAG", "counter :" + counter);


                customExpandableListViewAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

The out put is this: Out put

and i need Task1 the header tittle of first item and its contains 8 child's and like 2nd Task2 the header tittle of 2nd item and it will contains 5 child's and 3rd Task3 the header tittle of 3rd item and will contain's 10 child how can i achieved please help me i new to firebase Thanks...

1
  • Can you show your adapter class? Feb 8, 2018 at 8:59

1 Answer 1

3

At last i found the problem. the problem is that i create a single Object of ArrayList for child which runs 3 times and save all data in one Object, so i found out that i need to create ArrayList Object Accordingly to loop Turn's which is 3. this is the code which is working now.

myRef.addChildEventListener(new ChildEventListener() {
            int counter = 0;
            List<String> childItem;

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                listDataHeader.add(dataSnapshot.getKey());
                Log.e("TAG", listDataHeader.get(counter));
                childItem = new ArrayList<>();

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    String childNames = (String) ds.getValue();
                    Log.e("TAG", "childNames :" + childNames);
                    childItem.add(childNames);
                }

                listDataChild.put(listDataHeader.get(counter), childItem);
                counter++;
                Log.e("TAG", "counter :" + counter);

                customExpandableListViewAdapter.notifyDataSetChanged();
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.