@egorchikJava

Ошибка при приеме данных в nested recyclerview как исправить?

При запуске фрагмента где recycler выходит такая ошибка
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: mychati.app, PID: 26357
    com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(CustomClassMapper.java:251)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:177)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:102)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:229)
        at mychati.app.Client.repo.FirebaseRepo$1.onDataChange(FirebaseRepo.java:46)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8056)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

FirebaseRepo.
public class FirebaseRepo {
    private DatabaseReference databaseReference;
    private OnRealDbTaskComplete onRealDbTaskComplete;
    private FirebaseAuth mAuth;
    public FirebaseRepo(OnRealDbTaskComplete onRealDbTaskComplete){
        this.onRealDbTaskComplete=onRealDbTaskComplete;
        databaseReference= FirebaseDatabase.getInstance().getReference().child("DoCart");
        mAuth=FirebaseAuth.getInstance();

    }



    public void getAllData(){
        databaseReference.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List<ParentItem>parentItemList=new ArrayList<>();
                for (DataSnapshot ds:snapshot.getChildren()){
                    ParentItem parentItem=new ParentItem();
                    parentItem.setMagLogo(ds.child("MagLogo").getValue(String.class));
                    parentItem.setMagName(ds.child("MagName").getValue(String.class));


                    GenericTypeIndicator<ArrayList<ChildItem>> genericTypeIndicator=new GenericTypeIndicator<ArrayList<ChildItem>>() {
                    };
                    parentItem.setChildItemList(ds.getValue(genericTypeIndicator));
                    parentItemList.add(parentItem);
                }
                onRealDbTaskComplete.onSuccess(parentItemList);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
onRealDbTaskComplete.onFailure(error);
            }
        });
    }
    public interface OnRealDbTaskComplete{
        void onSuccess(List<ParentItem>parentItemList);
        void onFailure(DatabaseError error);
    }
}

ParentItem
public class ParentItem {
    private String MagLogo,MagName;

    private List<ChildItem> childItemList;
    
    public ParentItem(){}
    
    public ParentItem(String magLogo, String magName,List<ChildItem> childItemList) {
        MagLogo = magLogo;
        MagName = magName;
        this.childItemList = childItemList;
    }

    public List<ChildItem> getChildItemList() {
        return childItemList;
    }

    public void setChildItemList(List<ChildItem> childItemList) {
        this.childItemList = childItemList;
    }

    public String getMagLogo() {
        return MagLogo;
    }

    public void setMagLogo(String magLogo) {
        MagLogo = magLogo;
    }

    public String getMagName() {
        return MagName;
    }

    public void setMagName(String magName) {
        MagName = magName;
    }
}

ParentAdapter
public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ParentviewHolder> {
    private List<ParentItem>parentItemList;
    
    public void setParentItemList(List<ParentItem>parentItemList){
        this.parentItemList=parentItemList;
    }
    @NonNull
    @Override
    public ParentviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ParentviewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ParentviewHolder holder, int position) {
ParentItem parentItem=parentItemList.get(position);
        Transformation transformation=new RoundedTransformationBuilder().borderColor(Color.WHITE).borderWidthDp(3).cornerRadius(15).oval(false).build();


        Picasso.get().load(parentItem.getMagLogo()).transform(transformation).into(holder.roundedImagemaglogoparent);
        holder.nestedrecer.setHasFixedSize(true);
        holder.nestedrecer.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));

        ChildAdapter childAdapter=new ChildAdapter();
        childAdapter.setChildItemList(parentItem.getChildItemList());

        holder.nestedrecer.setAdapter(childAdapter);
        childAdapter.notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if (parentItemList !=null){
            return parentItemList.size();
        }else{
            return 0;
        }
    }
    public class ParentviewHolder extends RecyclerView.ViewHolder{
private RoundedImageView roundedImagemaglogoparent;
private TextView textitogParent;
private ImageView imagedeletemag;
private RecyclerView nestedrecer;
        public ParentviewHolder(@NonNull View itemView) {
            super(itemView);
            textitogParent=itemView.findViewById(R.id.textitogParent);
            roundedImagemaglogoparent=itemView.findViewById(R.id. roundedImagemaglogoparent);
            nestedrecer=itemView.findViewById(R.id.nestedrecer);
            imagedeletemag=itemView.findViewById(R.id.imagedeletemag);
        }
    }
}

ChildAdapter
public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> {
public List<ChildItem>childItemList;
public void setChildItemList(List<ChildItem>childItemList){
    this.childItemList=childItemList;
    this.childItemList.removeAll(Collections.singleton(null));
}
    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item,parent,false);
return new ChildViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
ChildItem childItem=childItemList.get(position);
holder.txttovarName.setText(childItem.getTovarname());
holder.txttovarValue.setText(childItem.getTovarValue());
holder.txttovarPrice.setText(childItem.getPrice());
holder.txttovarName.setHint(childItem.getTovarcartShopuid());
holder.txttovarPrice.setHint(childItem.getProductId());
        Transformation transformation=new RoundedTransformationBuilder().borderColor(Color.WHITE).borderWidthDp(3).cornerRadius(15).oval(false).build();
        Picasso.get().load(childItem.getTovarImage()).transform(transformation).into(holder.imagetovarchild);
    }
    @Override
    public int getItemCount() {
 if (childItemList !=null){
     return childItemList.size();
 }else {
     return 0;
 }
    }
    public class ChildViewHolder extends RecyclerView.ViewHolder{
private TextView txttovarName,txttovarValue,txttovarPrice,tovarplus,tovarminus;
private RoundedImageView imagetovarchild;
        public ChildViewHolder(@NonNull View itemView) {
            super(itemView);
            imagetovarchild=itemView.findViewById(R.id.imagetovarchild);
            txttovarName=itemView.findViewById(R.id.textnamechild);
            txttovarPrice=itemView.findViewById(R.id.textpricechildtovar);
            txttovarValue=itemView.findViewById(R.id.tovarcartchild);
            tovarminus=itemView.findViewById(R.id.tovarminuschildto);
            tovarplus=itemView.findViewById(R.id.tovarpluschildto);
        }
    }
}
  • Вопрос задан
  • 58 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы