将Firebase中的数据加载到ExpandableList

我试图在我的应用程序中使用可扩展列表。 我遵循这些步骤 ,但我坚持如何从Firebase检索数据,将它们添加为我的代码Dummydata。

这里是主要的活动

public class MainActivity extends AppCompatActivity { ExpandableListAdapter myAdapter; ExpandableListView myList; List<String> listDataHeader; HashMap<String,List<String>> listDataChild; private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mDatabaseReference; private ChildEventListener mChildEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get the listview myList=(ExpandableListView)findViewById(R.id.Explist); prepareListData(); myAdapter= new ExpandableListAdapter(this,listDataHeader,listDataChild); myList.setAdapter(myAdapter); } private void prepareListData(){ mFirebaseDatabase= FirebaseDatabase.getInstance(); mDatabaseReference = mFirebaseDatabase.getReference().child("category"); //Read Data from Database mChildEventListener = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { listDataHeader = new ArrayList<String>(); listDataChild = new HashMap<String, List<String>>(); listDataHeader=dataSnapshot.getValue(); /* * how to load data from firebase to the expandable * * * */ } @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) { } }; mDatabaseReference.addChildEventListener(mChildEventListener); } 

这里是适配器的代码

 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listDataChild; } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this._listDataChild.get(this._listDataHeader.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) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_gruop, null); } TextView lblListHeader = (TextView) convertView .findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if(convertView == null){ LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item, null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView;} @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } 

任何帮助表示赞赏。

在适配器中添加方法来更新列表并在列表中添加项目

  public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listDataChild; } public void addItemToList(String newItem){ this._listDataChild.plus(newItem); notifyDataSetChanged(); } public void setList(List<String> newList){ this._listDataChild = listDataChild; notifyDataSetChanged(); } ... 

并在Activity类中使用这些函数,,(在kotlin中将其转换为Java中的AS)

 /* * how to load data from firebase to the expandable * * * */ if (dataSnapshot != null) { val newList = arrayListOf<String>() for (listDataSnap in dataSnapshot.getChildren()) { val newItem: String? = listDataSnap.getValue() if (newItem != null) { newList.add(newItem) } } myAdapter?.setList(newList) } 

*重点是要明白! 干杯

Interesting Posts