java - Android Studio RecyclerView:没有附加适配器;跳过布局
问题描述
帮助问题是,运行应用程序时,我的 recyclerview 中没有出现任何内容,只有错误 android studio : E/RecyclerView: No adapter attach; 跳过布局
沙龙名单活动
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());
}
}
}
对不起我的英语我希望你能帮助我,我将非常感激
解决方案
该消息表示在呈现布局时未附加适配器。这是真的,因为您已将其附加到回调方法中
相反,如果您创建一个加载沙龙和适配器的字段,您可以立即附加它
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
更新它需要的适配器
推荐阅读
- web-scraping - 抓取点赞数最高的 Instagram 帐户图片
- java - 无法在 Java Swing 计算器中使用等号按钮
- javascript - 通过没有 jQuery 和远程的 AJAX 请求提交 Rails 表单:true
- html - html中搜索栏右侧不需要的空白
- php - 上传头像
- chef-infra - Chef Client 在 ubuntu 版本 14.04 上找不到 apt_update 的资源
- android - React native - 渲染数千个组件时的性能
- generics - 如何从扩展类型的 CrudRepository 调用 CrudRepository 上的泛型方法?
- android - React native drawer open issue
- mysql - Mysql 将字符串读取为日期时间