Java 未使用FirebaseDatabase在新片段中填充ExpandableListView
代码Java 未使用FirebaseDatabase在新片段中填充ExpandableListView,java,android,firebase,firebase-realtime-database,notifydatasetchanged,Java,Android,Firebase,Firebase Realtime Database,Notifydatasetchanged,代码 public class QuestInspectingFragment extends Fragment{ // public static ArrayList<String> mParentStepsList; public ArrayList<QuestObject> mRealQuests; public ArrayList<QuestObject> mQuestObjectList; ExpandableListView ExpListV
public class QuestInspectingFragment extends Fragment{
// public static ArrayList<String> mParentStepsList;
public ArrayList<QuestObject> mRealQuests;
public ArrayList<QuestObject> mQuestObjectList;
ExpandableListView ExpListView;
LinearLayout toolBarLayout;
ChildEventListener mStepsListener;
DatabaseReference mStepsReference ;
String referencePath;
QuestExpListAdapter questAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.quest_inspect_fragment,container,false);
referencePath = "versions/"+getArguments().getString("questPath")+"/Steps";
mStepsReference = FirebaseHelper.mDatabase.getReference().child(referencePath);
ExpListView = v.findViewById(R.id.ExpandableList);
mQuestObjectList = new ArrayList<>();
questAdapter = new QuestExpListAdapter(this.getContext(),mQuestObjectList);
createStepsListener();
ExpListView.setAdapter(questAdapter);
return v;
}
@Override
public void onResume() {
super.onResume();
mStepsReference.addChildEventListener(mStepsListener);
}
@Override
public void onPause() {
super.onPause();
mStepsReference.removeEventListener(mStepsListener);
}
public static Fragment createInspectFragment(String pathToQuest){
QuestInspectingFragment questInspectingFragment = new QuestInspectingFragment();
Bundle myBundle = new Bundle();
myBundle.putString("questPath",pathToQuest);
questInspectingFragment.setArguments(myBundle);
return questInspectingFragment;
}
public void createStepsListener(){
mStepsListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//System.out.println((String)dataSnapshot.getKey()+"\n \n \n \n \n \n");
QuestObject mQuest = new QuestObject();
//Populate Step Items
if(dataSnapshot.hasChild("title")){
mQuest.setmStepTitleList((String)dataSnapshot.child("title").getValue());
}else {
mQuest.setmStepTitleList((dataSnapshot.getKey().toUpperCase()));
}
//Populate Substeps Items
if (dataSnapshot.hasChild("Substeps")){
for(DataSnapshot childSnapshot : dataSnapshot.child("Substeps").getChildren()){
System.out.println((String)childSnapshot.getKey()+"\n \n \n \n \n \n");
if(childSnapshot.hasChild("title")){
mQuest.mSubstepTitle.add ((String)childSnapshot.child("title").getValue());
;
}
//populate subsubsteps
if(childSnapshot.hasChild("subsubstep")){
QuestObject.SubsubstepList mList = new QuestObject.SubsubstepList();
for(DataSnapshot subsubstep : childSnapshot.child("subsubstep").getChildren()){
mList.mclassSubstepList.add((String) subsubstep.child("title").getValue());
}
mQuest.mSubSubstep.add(mList);
}
}}
mQuestObjectList.add(mQuest);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
}
public类QuestInspectingFragment扩展了片段{
//公共静态数组列表mParentStepsList;
公共阵列列表请求;
公共ArrayList mQuestObjectList;
ExpandableListView解释视图;
线性布局;
ChildEventListener mStepsListener;
数据库引用mStepsReference;
字符串引用路径;
questexplistapter questAdapter;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图v=充气机。充气(右布局。任务检查碎片,容器,假);
referencePath=“versions/”+getArguments().getString(“questPath”)+“/Steps”;
mStepsReference=firebaseheloper.mDatabase.getReference().child(referencePath);
ExpListView=v.findviewbyd(R.id.ExpandableList);
mQuestObjectList=新的ArrayList();
questAdapter=new QuestExpListAdapter(this.getContext(),mQuestObjectList);
createStepsListener();
ExpListView.setAdapter(questAdapter);
返回v;
}
@凌驾
恢复时公开作废(){
super.onResume();
mStepsReference.addChildEventListener(mStepsListener);
}
@凌驾
公共无效暂停(){
super.onPause();
mStepsReference.removeEventListener(mStepsListener);
}
公共静态片段createInspectFragment(字符串pathToQuest){
QuestInspectingFragment QuestInspectingFragment=新的QuestInspectingFragment();
Bundle myBundle=新Bundle();
putString(“questPath”,pathToQuest);
setArguments(myBundle);
返回QuesteInspectingFragment;
}
公共无效createStepsListener(){
mStepsListener=新的ChildEventListener(){
@凌驾
公共void onChildaded(@NonNull DataSnapshot DataSnapshot,@null字符串s){
//System.out.println((字符串)dataSnapshot.getKey()+“\n\n\n\n\n”);
QuestObject mQuest=新的QuestObject();
//填充步骤项
if(dataSnapshot.hasChild(“标题”)){
mQuest.setmStepTitleList((字符串)dataSnapshot.child(“title”).getValue();
}否则{
mQuest.setmsteptilelist((dataSnapshot.getKey().toUpperCase());
}
//填充子步骤项
if(dataSnapshot.hasChild(“子步”)){
for(DataSnapshot childSnapshot:DataSnapshot.child(“子步骤”).getChildren(){
System.out.println((字符串)childSnapshot.getKey()+“\n\n\n\n”);
if(childSnapshot.hasChild(“标题”)){
mQuest.mSubstepTitle.add((字符串)childSnapshot.child(“title”).getValue());
;
}
//填充子步骤
if(childSnapshot.hasChild(“子步骤”)){
QuestObject.SubsubstepList mList=新的QuestObject.substeplist();
for(DataSnapshot子步骤:childSnapshot.child(“子步骤”).getChildren()){
mList.mclassSubstepList.add((字符串)substep.child(“title”).getValue());
}
mQuest.mSubSubstep.add(mList);
}
}}
添加(mQuest);
}
@凌驾
public void onChildChanged(@NonNull DataSnapshot DataSnapshot,@null字符串s){
}
@凌驾
公共void onChildRemoved(@NonNull DataSnapshot DataSnapshot){
}
@凌驾
public void onChildMoved(@NonNull DataSnapshot DataSnapshot,@null字符串s){
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
};
}
}
CustomExpandableListAdapter
public class QuestExpListAdapter extends BaseExpandableListAdapter {
private Context mContext;
ArrayList<QuestObject> mQuestsList;
public QuestExpListAdapter(Context context,ArrayList<QuestObject> newQuestList){
mContext = context;
this.mQuestsList = newQuestList;
}
@Override
public int getGroupCount() {
return mQuestsList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder mvh = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.quest_exp_row1,null);
mvh = new MyViewHolder(row);
row.setTag(mvh);
}else {
mvh = (MyViewHolder) row.getTag();
}
mvh.step.setText("Step "+(groupPosition+1)+":"+mQuestsList.get(groupPosition).mStepTitleList);
return row;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
SubstepExpandableList substepExpList = new SubstepExpandableList(mContext);
substepExpList.setAdapter(new SubstepListAdapter(mContext, mQuestsList.get(groupPosition).mSubstepTitle, mQuestsList.get(groupPosition).mSubSubstep));
substepExpList.setGroupIndicator(null);
return substepExpList;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class SubstepExpandableList extends ExpandableListView {
public SubstepExpandableList(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SubstepListAdapter extends BaseExpandableListAdapter{
ArrayList<String> myPassedList;
ArrayList<QuestObject.SubsubstepList> mySecondList;
private Context mmContext;
public SubstepListAdapter(Context context, ArrayList<String> myPassedList,ArrayList<QuestObject.SubsubstepList> myList ){
mmContext = context;
this.myPassedList = myPassedList;
this.mySecondList = myList;
}
@Override
public int getGroupCount() {
return myPassedList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
try {
return mySecondList.get(groupPosition).mclassSubstepList.size();
}catch (IndexOutOfBoundsException e){
Toast.makeText(mmContext,"No Further Steps",Toast.LENGTH_SHORT).show();
return 0;
}
//else {
// Toast.makeText(mmContext,"No Further Steps",Toast.LENGTH_SHORT).show();
// return 1;
// }
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder mvh = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.quest_exp_row2,null);
mvh = new MyViewHolder(row);
row.setTag(mvh);
}else {
mvh = (MyViewHolder) row.getTag();
}
mvh.substep.setText(myPassedList.get(groupPosition));
return row;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder mvh = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.quest_exp_row3,null);
mvh = new MyViewHolder(row);
row.setTag(mvh);
}else {
mvh = (MyViewHolder) row.getTag();
}
if(mySecondList.get(groupPosition)!= null ) {
if (mySecondList.get(groupPosition).mclassSubstepList.size() !=0){
mvh.subsub.setText(mySecondList.get(groupPosition).mclassSubstepList.get(childPosition));
}}
return row;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
private class MyViewHolder{
TextView step;
TextView substep;
TextView subsub;
MyViewHolder(View v){
step = v.findViewById(R.id.questRow1Text);
substep = v.findViewById(R.id.questRow2Text);
subsub =v.findViewById(R.id.questRow3Text);
}
}
公共类QuesteExplistAdapter扩展了BaseExpandableListAdapter{
私有上下文;
ArrayList mQuestsList;
公共QuestExpListAdapter(上下文上下文,ArrayList newQuestList){
mContext=上下文;
this.mQuestsList=newQuestList;
}
@凌驾
public int getGroupCount(){
返回mQuestsList.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回1;
}
@凌驾
公共对象getGroup(int-groupPosition){
返回组位置;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回0;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
视图行=转换视图;
MyViewHolder mvh=null;
if(行==null){
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
行=充气机。充气(R.layout.quest\u exp\u行1,空);
mvh=新的MyViewHolder(行);
行设置标签(mvh);
}否则{
mvh=(MyViewHolder)row.getTag();
}
mvh.step.setText(“step”+(groupPosition+1)+”:“+mQuestsList.get(groupPosition.msteptilelist);
返回行;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
SubstepExpandableList substepExpList=新的SubstepExpandableList(mContext);
setAdapter(新的SubstepListAdapter(mContext,mQuestsList.get(groupPosition).mSubstepTitle,mQuestsList.get(groupPosition.msubstep));
substepExpList.setGroupIndicator(null);
返回子步骤列表;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
公共类SubstepExpandableList扩展了ExpandableListView{
公共子步骤可扩展列表(上下文){
超级(上下文);
}
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
public class QuestObject {
public void setmStepTitleList(String mStepTitleList) {
this.mStepTitleList = mStepTitleList;
}
public String mStepTitleList ;
public ArrayList<String> mSubstepTitle = new ArrayList<>();
public ArrayList<SubsubstepList> mSubSubstep = new ArrayList<>();
public static class SubsubstepList{
ArrayList<String> mclassSubstepList = new ArrayList<>();
}
mStepsListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//System.out.println((String)dataSnapshot.getKey()+"\n \n \n \n \n \n");
QuestObject mQuest = new QuestObject();
//Populate Step Items
if(dataSnapshot.hasChild("title")){
mQuest.setmStepTitleList((String)dataSnapshot.child("title").getValue());
}else {
mQuest.setmStepTitleList((dataSnapshot.getKey().toUpperCase()));
}
//Populate Substeps Items
if (dataSnapshot.hasChild("Substeps")){
for(DataSnapshot childSnapshot : dataSnapshot.child("Substeps").getChildren()){
System.out.println((String)childSnapshot.getKey()+"\n \n \n \n \n \n");
if(childSnapshot.hasChild("title")){
mQuest.mSubstepTitle.add ((String)childSnapshot.child("title").getValue());
//mSubstepList.add((String)childSnapshot.child("title").getValue());
}
//populate subsubsteps
if(childSnapshot.hasChild("subsubstep")){
QuestObject.SubsubstepList mList = new QuestObject.SubsubstepList();
for(DataSnapshot subsubstep : childSnapshot.child("subsubstep").getChildren()){
mList.mclassSubstepList.add((String) subsubstep.child("title").getValue());
// mSubSubStepList.add((String) subsubstep.child("title").getValue());
}
mQuest.mSubSubstep.add(mList);
}
}}
mQuestObjectList.add(mQuest);
**questAdapter.notifyDataSetChanged();**
}
public void onResume() {
//mQuestObjectList = new ArrayList<>();
super.onResume();
mStepsReference.addChildEventListener(mStepsListener);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
questAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onPause() {
questAdapter.mQuestsList.clear();
super.onPause();
mStepsReference.removeEventListener(mStepsListener);
}