英文:
RecyclerView Error No adapter attached ; skipping layout and setting OnClickListener
问题
我正在开发一个应用程序,在其中将数据存储在Firebase上,并在用户成功登录后检索数据。应用程序工作正常,但我看到出现了以下错误。
E/RecyclerView: No adapter attached; skipping layout
我正在加载一张图片以及长短描述。我试图将用户带到另一个活动,他们可以在那里更详细地查看内容。然而,我困惑的是我应该在哪里设置OnClickListener,以便我可以获取此帖子的ID,并在另一个活动中加载它。
这是主活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
homeAuth = FirebaseAuth.getInstance();
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
progressDialog = new ProgressDialog(this);
recyclerView.setLayoutManager(layoutManager);
postModelList = new ArrayList<>();
pullPosts();
}
这是从数据库中提取数据的方法:
void pullPosts() {
progressDialog.setMessage("Please wait for the pictures to load...");
progressDialog.show();
progressDialog.setCancelable(false);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postModelList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()){
postModel = ds.getValue(PostModel.class);
postModelList.add(postModel);
postAdapter = new PostAdapter(HomeActivity.this, postModelList);
recyclerView.setAdapter(postAdapter);
}
progressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
Toast.makeText(HomeActivity.this, "" + databaseError, Toast.LENGTH_SHORT).show();
}
});
}
我阅读了其他与该错误相关的帖子,尝试了各种方法,但其余部分在我看来都没问题,以下是我的适配器:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyHolder> {
// 适配器的内容已省略
}
你可以帮我修复这个问题吗?非常感谢你的时间和帮助。
英文:
I'm working on an application where I store the data on Firebase and retrieve it after the user manages to sign in. The application works fine I see this error occurs.
E/RecyclerView: No adapter attached; skipping layout
I'm loading an image together with long and short description. I'm trying to take the users into another Activity where they can the content more detailed. However. I am confused where I can set OnClickListener so that I can take the id of this post and load it in another activity.
this is the main activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
homeAuth = FirebaseAuth.getInstance();
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true); // this will load it from the end and show last as first
progressDialog = new ProgressDialog(this);
recyclerView.setLayoutManager(layoutManager);
postModelList = new ArrayList<>();
pullPosts();
}
and this is the method where I pull the data from the DB :
void pullPosts() {
progressDialog.setMessage("Please wait for the pictures to load...");
progressDialog.show();
progressDialog.setCancelable(false);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postModelList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()){
postModel = ds.getValue(PostModel.class);
postModelList.add(postModel);
postAdapter = new PostAdapter(HomeActivity.this , postModelList);
recyclerView.setAdapter(postAdapter);
}
progressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
Toast.makeText(HomeActivity.this, ""+databaseError, Toast.LENGTH_SHORT).show();
}
});
}
I read other posts related to that error tried the options but the rest seems to me fine as the following is my Adapter :
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyHolder> {
Context context;
List<PostModel> postModelList;
public PostAdapter(Context context, List<PostModel> postModelList) {
this.context = context;
this.postModelList = postModelList;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.home_post, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
String title = postModelList.get(position).getPostTitle();
String sDescription = postModelList.get(position).getPostShDescription();
String lDescription = postModelList.get(position).getPostLoDescription();
String image = postModelList.get(position).getPostImage();
holder.pulledTitle.setText(title);
holder.pulledShortDescription.setText(sDescription);
holder.pulledLongDescription.setText(lDescription);
Glide.with(context).load(image).into(holder.pulledImage);
//now we will add library to load image
}
@Override
public int getItemCount() {
return postModelList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView pulledImage;
TextView pulledTitle, pulledShortDescription, pulledLongDescription;
public MyHolder(@NonNull View itemView) {
super(itemView);
pulledImage = itemView.findViewById(R.id.pulled_image);
pulledTitle = itemView.findViewById(R.id.pulled_title);
pulledShortDescription = itemView.findViewById(R.id.pulled_short_description);
pulledLongDescription = itemView.findViewById(R.id.pulled_long_description);
}
}
}
Could you please help me with fixing the issue? I would appreciate your time and help.
答案1
得分: 0
E/RecyclerView: 未附加适配器;跳过布局
这不是一个错误,你可以忽略它,这是因为在活动创建时没有适配器,因为你是在获取数据之后设置适配器。
关于你的问题,在哪里设置点击监听器,你应该在适配器的 viewHolder 中进行设置。
class MyHolder extends RecyclerView.ViewHolder {
ImageView pulledImage;
TextView pulledTitle, pulledShortDescription, pulledLongDescription;
public MyHolder(@NonNull View itemView) {
super(itemView);
pulledImage = itemView.findViewById(R.id.pulled_image);
pulledTitle = itemView.findViewById(R.id.pulled_title);
pulledShortDescription = itemView.findViewById(R.id.pulled_short_description);
pulledLongDescription = itemView.findViewById(R.id.pulled_long_description);
itemView.setOnClickListener(v -> {
// 在这里实现你的逻辑,例如:Id = postModelList.getId(); 或者从这里启动另一个活动
});
}
}
英文:
E/RecyclerView: No adapter attached; skipping layout
that's not an error you can ignore it, it's happening because there's not adapter when the activity is created as you set the adapter after fetching the data.
About your question where to set onClickListener, you should do that in the adapter's viewHolder
class MyHolder extends RecyclerView.ViewHolder {
ImageView pulledImage;
TextView pulledTitle, pulledShortDescription, pulledLongDescription;
public MyHolder(@NonNull View itemView) {
super(itemView);
pulledImage = itemView.findViewById(R.id.pulled_image);
pulledTitle = itemView.findViewById(R.id.pulled_title);
pulledShortDescription = itemView.findViewById(R.id.pulled_short_description);
pulledLongDescription = itemView.findViewById(R.id.pulled_long_description);
itemView.setOnClickListner(v->{
//your implementation here f.e Id = postModelList.getId(); or and start another activity from here
});
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论