0

当我放入 Firebase 的 addChildEventListener -> onChildAdded 覆盖函数时,以下代码正在工作,而不是 myRecyclerViewAdapter.notifyDataSetChanged()

expendableRecyclerViewAdapter = new ExpendableRecyclerViewAdapter(listOfItems, getContext());
mExpandableRecyclerView.setAdapter(expendableRecyclerViewAdapter);

以下功能正在工作'未连接适配器; 跳过布局'错误正在显示在 logcat 中这是什么问题?任何帮助表示赞赏

    private boolean getCurrentRestMenuList(){
        DatabaseReference databaseReference = firebaseDatabase.getReference("menus").child(restID);
        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                MenuModelForFirebase menuModel = dataSnapshot.getValue(MenuModelForFirebase.class);
                MenuModel expandableMenuModel = new MenuModel(menuModel.getMenuTitle(), menuModel.getFoodModelList());
                listOfMenus.add(expandableMenuModel);

                expendableRecyclerViewAdapter = new ExpendableRecyclerViewAdapter(listOfMenus, getContext());
                mExpandableRecyclerView.setAdapter(expendableRecyclerViewAdapter);


            }

            @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) {

            }
        });
        return listOfMenus.isEmpty();
}

在 Firebase 的 addchildeventlistener -> onChildAdded 中调用 RecyclerViewAdapter.notifyDataSetChanged() 时出错

01-04 06:48:31.907 30495-30495/com.example.aliy.bisp_client_side E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.aliy.bisp_client_side, PID: 30495
                                                                               java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                                   at com.thoughtbot.expandablerecyclerview.models.ExpandableList.numberOfVisibleItemsInGroup(ExpandableList.java:37)
                                                                                   at com.thoughtbot.expandablerecyclerview.models.ExpandableList.getVisibleItemCount(ExpandableList.java:50)
                                                                                   at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.getItemCount(ExpandableRecyclerViewAdapter.java:93)

Firebase 的菜单模型

public class MenuModelForFirebase {

private String MenuTitle;
private List<FoodModel> foodModelList;

public MenuModelForFirebase(){}

public MenuModelForFirebase(String menuTitle, List<FoodModel> foodModelList) {
    MenuTitle = menuTitle;
    this.foodModelList = foodModelList;
}

public String getMenuTitle() {
    return MenuTitle;
}

public void setMenuTitle(String menuTitle) {
    MenuTitle = menuTitle;
}

public List<FoodModel> getFoodModelList() {
    return foodModelList;
}

public void setFoodModelList(List<FoodModel> foodModelList) {
    this.foodModelList = foodModelList;
}

}

食物模型

    package com.example.aliy.bisp_client_side.Models;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by Aliy on 12/31/2017.
 */

public class FoodModel implements Parcelable {



private String fId;
private String fName;
private int fPrice;
private String fDescription;
private String fCuisineType;
private String fFoodImage;


public FoodModel(String fId, String fName, int fPrice, String description, String fCuisineType, String fFoodImage) {
    this.fId = fId;
    this.fName = fName;
    this.fPrice = fPrice;
    this.fDescription = description;
    this.fCuisineType = fCuisineType;
    this.fFoodImage = fFoodImage;
}

public FoodModel() {

}


protected FoodModel(Parcel in) {
    fId = in.readString();
    fName = in.readString();
    fPrice = in.readInt();
    fDescription = in.readString();
    fCuisineType = in.readString();
    fFoodImage = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(fId);
    parcel.writeString(fName);
    parcel.writeInt(fPrice);
    parcel.writeString(fDescription);
    parcel.writeString(fCuisineType);
    parcel.writeString(fFoodImage);
}

public static final Creator<FoodModel> CREATOR = new Creator<FoodModel>() {
    @Override
    public FoodModel createFromParcel(Parcel in) {
        return new FoodModel(in);
    }

    @Override
    public FoodModel[] newArray(int size) {
        return new FoodModel[size];
    }
};



public String getfId() {
    return fId;
}

public String getfName() {
    return fName;
}

public int getfPrice() {
    return fPrice;
}

public String getfDescription() {
    return fDescription;
}

public String getfCuisineType() {
    return fCuisineType;
}

public String getfFoodImage() {
    return fFoodImage;
}

public void setfFoodImage(String fFoodImage) {
    this.fFoodImage = fFoodImage;
}

public void setfCuisineType(String fCuisineType) {
    this.fCuisineType = fCuisineType;
}

public void setfId(String fId) {
    this.fId = fId;
}

public void setfName(String fName) {
    this.fName = fName;
}

public void setfPrice(int fPrice) {
    this.fPrice = fPrice;
}

public void setfDescription(String fDescription) {
    this.fDescription = fDescription;
}
}

可扩展回收站视图的菜单模型

public class MenuModel extends ExpandableGroup<FoodModel> {

public MenuModel(String title, List<FoodModel> items) {
    super(title, items);
}

}

具有可扩展回收器视图的片段

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_restaurant__menu, container, false);
    SetRestID();
    //Set visible Brand Icon and Restaurant Imageview layouts
    ((Home_Screen)getActivity()).showImageviewAndBrandIcon();


    //region TITLE INIT
    collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapsingToolbarLayout);
    //endregion

    //region CHANGE TITLE TEXTVIEW COLORS
    collapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.PALETTE_AMBER));
    collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.white));
    //endregion

    //region APP BAR LAYOUT WITH SETTING BEHAVIOUR TO BRAND ICON
    appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.main_home_page_app_bar_layout);
    appBarLayout.addOnOffsetChangedListener(this);
    //endregion

    //region BRAND ICON LAYOUT INIT
    brand_icon = (LinearLayout) getActivity().findViewById(R.id.brand_icon);
    YoYo.with(Techniques.ZoomInUp).duration(2000).playOn(brand_icon);
    //endregion

    //region FIREBASE INITIALIZING
    firebaseDatabase = FirebaseDatabase.getInstance();
    //endregion

    //region SETTING UP IMAGEVIEWS and Textviews
    SetupLayout(view);
    //endregion

    //region GET RESTAURANT DATA FROM DB
    getCurrentRestaurantInfo();
    //endregion



    //region CONTENT PART -- RETRIEVING RESTAURANTS MENUS FROM DB
    mExpandableRecyclerView =  view.findViewById(R.id.expandable_recycler_menu);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setAutoMeasureEnabled(true);
    mExpandableRecyclerView.setLayoutManager(layoutManager);

    listOfMenus= new ArrayList<>();
    mExpandableRecyclerView.setNestedScrollingEnabled(false);
    getCurrentRestMenuList();
    //endregion

    //region SETUP DEFAULT IMAGES TO SCREEN
    setupImageViewToDefaultImages();
    //endregion

    return view;
}
4

1 回答 1

0

你应该在这里了解两件事,

  1. ChildEventListener 是异步的。您不能根据从 childeventlistener 中检索到的值返回true或返回函数。false

  2. 每次为 recyclerview 更新数据集时,您不必附加适配器的新对象。

以下是我建议的更改:

List<MenuModel> listOfMenus = new ArrayList();
expendableRecyclerViewAdapter = new ExpendableRecyclerViewAdapter(listOfMenus, getContext());
mExpandableRecyclerView.setAdapter(expendableRecyclerViewAdapter);
private void getCurrentRestMenuList(){
    listOfMenus.clear();
    DatabaseReference databaseReference = firebaseDatabase.getReference("menus").child(restID);
    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            MenuModelForFirebase menuModel = dataSnapshot.getValue(MenuModelForFirebase.class);
            MenuModel expandableMenuModel = new MenuModel(menuModel.getMenuTitle(), menuModel.getFoodModelList());
            listOfMenus.add(expandableMenuModel);
            expendableRecyclerViewAdapter.notifyDataSetChanged();// This will notify the adapter to reload the values for update the recyclerview.



        }

        @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) {

        }
    });
    //return listOfMenus.isEmpty();It might always return false because childeventlistener is an async. call. I don't see the use of this. 
}

如果您有特定的用例,请将其添加到问题中。

于 2018-01-04T05:48:00.797 回答