尝试使用ExpendableListView来显示firebase数据,但它没有显示任何内容,当使用静态数据时它正在工作。 my data,还是有其他方法可以列出像ExpendableListView这样的数据,但可以使用firebase。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_appointment, container, false);
if (user != null) {
buttonAppoint = view.findViewById(R.id.bappoint);
buttonAppoint.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Fragment newFragment = new Register();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flContent, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
// get the listview
expListView = view.findViewById(R.id.explist);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this.getContext(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
Toast.makeText(getActivity(), "Please login before view/make appointment",
Toast.LENGTH_SHORT).show();
Fragment newFragment = new LoginFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flContent, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Log.d(TAG, "onAuthStateChanged:signed_out");
}
return view;
}
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
myRef.orderByChild("userID").equalTo(currentUser).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Appointment appointment = dataSnapshot.getValue(Appointment.class);
listDataHeader.add(appointment.date+" "+ appointment.time);
List<String> appoint = new ArrayList<>();
appoint.add("Hours of Service: "+ appointment.hours);
appoint.add("Person In Charge: "+ appointment.personInCharge);
appoint.add("Address: "+ appointment.address);
appoint.add("Contact: "+ appointment.contact);
listDataChild.put(listDataHeader.get(i), appoint); // Header, Child data
i++;
listAdapter.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) {
}
});
}