简体   繁体   中英

Android Studio RecyclerView: No adapter attached; skipping layout

Help the problem is that when running the application nothing appears in my recyclerview, only error android studio : E/RecyclerView: No adapter attached; skipping layout

Salon ListActivity

public class SalonListActivity extends AppCompatActivity implements IOnLoadCountSalon, IBranchLoadListener {


IOnLoadCountSalon iOnLoadCountSalon;
IBranchLoadListener iBranchLoadListener;
AlertDialog dialog;

@BindView(R.id.txt_salon_count)
TextView txt_salon_count;

@BindView(R.id.recycler_salon)
RecyclerView recycler_salon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_salon_list);
    ButterKnife.bind(this);
    initView();
    init();
    loadSalonBaseOnCity(Common.state_name);
}

private void loadSalonBaseOnCity(String name) {
    dialog.show();
    FirebaseFirestore.getInstance().collection("AllSalon")
            .document(name)
            .collection("Branch")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful())
                    {
                        List<Salon> salons = new ArrayList<>();
                        iOnLoadCountSalon.onLoadCountSalonSuccess(task.getResult().size());
                        for(DocumentSnapshot salonSnapShot : task.getResult())
                        {
                            Salon salon = salonSnapShot.toObject(Salon.class);
                            salons.add(salon);
                        }
                        iBranchLoadListener.onBranchLoadSuccess(salons);
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            iBranchLoadListener.onBranchLoadFailed(e.getMessage());
        }
    });
}

private void init() {
    dialog = new SpotsDialog.Builder().setContext(this).setCancelable(false).build();
    iOnLoadCountSalon=this;
    iBranchLoadListener = this;
}

private void initView() {
    recycler_salon.setHasFixedSize(true);
    recycler_salon.setLayoutManager(new GridLayoutManager(this,2));
    recycler_salon.addItemDecoration(new SpacesItemDecoration(8));
}

@Override
public void onLoadCountSalonSuccess(int count) {
    txt_salon_count.setText(new StringBuilder("Cantidad Salones (").append(count).append(")"));
}

@Override
public void onBranchLoadSuccess(List<Salon> salonList) {
    MySalonAdapter salonAdapter = new MySalonAdapter(this,salonList);
    recycler_salon.setAdapter(salonAdapter);
    dialog.dismiss();
}

@Override
public void onBranchLoadFailed(String message) {
    Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
    dialog.dismiss();
}}`

Salon Adapter

public class MySalonAdapter extends RecyclerView.Adapter<MySalonAdapter.MyViewHolder> {

    Context context;
    List<Salon> salonList;
    List<CardView> cardViewList;
    LocalBroadcastManager localBroadcastManager;

    public MySalonAdapter(Context context, List<Salon> salonList) {
        this.context = context;
        this.salonList = salonList;
        cardViewList = new ArrayList<>();
        localBroadcastManager = LocalBroadcastManager.getInstance(context);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.layout_salon, viewGroup, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
        myViewHolder.txt_salon_name.setText(salonList.get(i).getName());
        myViewHolder.txt_salon_address.setText(salonList.get(i).getAddress());
        if (!cardViewList.contains(myViewHolder.card_salon))
            cardViewList.add(myViewHolder.card_salon);

        myViewHolder.setiRecyckerItemSelectedListener(new IRecyclerItemSelectedListener() {
            @Override
            public void onItemSelected(View view, int position) {

            }

        });
    }

    @Override
    public int getItemCount() {
        return salonList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        IRecyclerItemSelectedListener iRecyckerItemSelectedListener;
        TextView txt_salon_name, txt_salon_address;
        CardView card_salon;

        public void setiRecyckerItemSelectedListener(IRecyclerItemSelectedListener iRecyckerItemSelectedListener) {
            this.iRecyckerItemSelectedListener = iRecyckerItemSelectedListener;
        }

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            card_salon = (CardView) itemView.findViewById(R.id.card_salon);
            txt_salon_name = (TextView) itemView.findViewById(R.id.txt_salon_name);
            txt_salon_address = (TextView) itemView.findViewById(R.id.txt_salon_address);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            iRecyckerItemSelectedListener.onItemSelected(v, getAdapterPosition());
        }
    }
}

Sorry for my English I hope you can help me, I will be very grateful

The message is saying that the adapter is not attached at the time the layout is rendered. Which is true because you've attached it within a callback method

Instead, you can attach it immediately if you create a field of loaded salons and the adapter

IBranchLoadListener iBranchLoadListener;
List<Salon> mSalons = new ArrayList<>();
RecyclerView.Adapter<MySalonAdapter.MyViewHolder> mAdapter;

Use that list in the callback rather than make a new list

                 if(task.isSuccessful())
                {
                    mSalons.clear();
                    List<DocumentSnapshot> result = task.getResult();
                    iOnLoadCountSalon.onLoadCountSalonSuccess(result.size());
                    for(DocumentSnapshot salonSnapShot : result)
                    {
                        Salon salon = salonSnapShot.toObject(Salon.class);
                        mSalons.add(salon);
                    }
                    iBranchLoadListener.onBranchLoadSuccess(mSalons);
                }

And then initialize and attach your adapter immediately within the onCreate or init methods rather than wait for the callback

Within onBranchLoadSuccess you should notify the adapter that it needs updated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM