开发者社区> 问答> 正文

Android Studio RecyclerView:未连接适配器;跳过布局

帮助的问题是,在运行该应用程序时,我的recyclerview中什么也没有出现,只有错误android studio:E / RecyclerView:未连接适配器;跳过布局

沙龙列表活动

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();
}}`

沙龙适配器

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());
        }
    }
}

对不起,我的英语,希望您能对我有所帮助,我将非常感谢

展开
收起
垚tutu 2019-12-04 17:20:03 1315 0
1 条回答
写回答
取消 提交回答
  • #include

    该消息表示在渲染布局时未连接适配器。这是正确的,因为您已将其附加到回调方法中

    相反,如果您创建了一个加载沙龙和适配器的字段,则可以立即附加它

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

    在回调中使用该列表,而不要创建新列表

    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);
                    }
    
    

    然后立即在onCreate或init方法中初始化并附加适配器,而不是等待回调

    在onBranchLoadSuccess您应该notify适配器,它需要更新

    2019-12-04 17:20:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载