It shows that the problem is here:
@Override
public int getChildrenCount(int groupPosition) {
return hashMap.get(groupPosition).size();
}
I tried to print the map size and it does. It has only two keys: "Upcoming Events" and "Past Events". I checked using the containsKey() and the result was true. But somehow I still can't get the size of the List object contained as a value.
It may help to know that I did not create another class file for this and all the code is bundled up in the same class file.
Here's my code:
class CustomEventListAdapter extends BaseExpandableListAdapter{
HashMap<String, List<Event>> hashMap;
Context con;
CustomEventListAdapter(Context context, HashMap<String, List<Event>> map){
hashMap = map;
con = context;
}
List<String> groupTitle = Arrays.asList("Upcoming Events","Past Events");
@Override
public int getGroupCount() {
return groupTitle.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return hashMap.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupTitle.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return hashMap.get(groupTitle.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.groupHeader);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(groupTitle.get(groupPosition));
return convertView; }
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
}
TextView eventName = (TextView) convertView.findViewById(R.id.eventName);
TextView eventDate = (TextView) convertView.findViewById(R.id.eventDate);
TextView eventDesc = (TextView) convertView.findViewById(R.id.eventDesc);
ImageView imageView = (ImageView) convertView.findViewById(R.id.eventPic);
eventName.setText(hashMap.get(groupPosition).get(childPosition).getName());
eventDate.setText(hashMap.get(groupPosition).get(childPosition).getDate());
eventDesc.setText(hashMap.get(groupPosition).get(childPosition).getDetails());
return convertView; }
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
This is the onCreate() :
expandableListView = (ExpandableListView) findViewById(R.id.eventList);
hashMap = new HashMap<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference().child("Events");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<Event> eventList = new ArrayList<>();
//List<Event> upcoming = new ArrayList<>();
//List<Event> past = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Event event = new Event();
event.name = String.valueOf(postSnapshot.child("Name").getValue());
event.details = String.valueOf(postSnapshot.child("Details").getValue());
event.add = String.valueOf(postSnapshot.child("PhotoAdd").getValue());
event.date = String.valueOf(postSnapshot.child("Date").getValue());
eventList.add(event);
Toast.makeText(EventsActivity.this, "longDate = " + event.getLongDate() , Toast.LENGTH_SHORT).show();
}
long current = Calendar.getInstance().getTimeInMillis();
Collections.sort(eventList, new Sortbyroll());
int i=0;
for( i=0; i< eventList.size(); i++){
if(eventList.get(i).getLongDate() < current){
break;
}
}
hashMap.put("Upcoming Events", eventList.subList(0,i));
hashMap.put("Past Events", eventList.subList(i,eventList.size()));
ExpandableListAdapter listAdapter = new CustomEventListAdapter(getBaseContext(),hashMap);
expandableListView.setAdapter(listAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});