英文:
Unable to get the Cardview, E/RecyclerView: No adapter attached; skipping layout error , how to add adapter to recyclerView?
问题
以下是你提供的代码的中文翻译:
我正在使用Android Studio构建一个应用程序。在运行应用程序时,我遇到了“E/RecyclerView: No adapter attached; skipping layout”的问题。
由于我对Android Studio开发还很新,无法找出问题所在。我尝试了一些在线解决方案,但对我没有起作用。
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
DatabaseReference mDatabase;
ProgressDialog progressDialog;
private List<Upload> uploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview_id);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
progressDialog.setMessage("请稍候...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("upload");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressDialog.dismiss();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload = postsnapshot.getValue(Upload.class);
uploads.add(upload);
}
adapter = new RecyclerViewAdapter(getApplicationContext(), uploads);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
**适配器**
这是我的适配器代码,有时候会出错,或者我可能犯了一个错误,请检查这两个地方,并给我一个纠正这个问题的想法。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<Upload> uploads;
public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}
}
英文:
I'm using the android studio to build an application. I'm getting "E/RecyclerView: No adapter attached; skipping layout" when running the app.
Since I'm new to android studio development, unable to figure out the problem. I tried several solutions online, but it didn't work for me.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
DatabaseReference mDatabase;
ProgressDialog progressDialog;
private List <Upload> uploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview_id);
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
progressDialog.setMessage("Please wait ...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("upload");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressDialog.dismiss();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload = postsnapshot.getValue(Upload.class);
uploads.add(upload);
}
adapter = new RecyclerViewAdapter(getApplicationContext(),uploads);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
Adapter
This is my adapter code, sometime it will be wrong or I had a mistake check these two and please give me an idea to correct this problem.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
private Context mContext;
private List <Upload> uploads;
public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}
}
答案1
得分: 1
你需要在 onCreate
方法中设置适配器,目前你是在监听器中设置的。我猜想你只需要将 recylerView.setAdapter(adapter)
移动到 onCreate
中,然后在你的监听器中设置数据和调用 notifyDataSetChanged
。
应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview_id);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
adapter = new RecyclerViewAdapter(getApplicationContext());
recyclerView.setAdapter(adapter);
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
progressDialog.setMessage("请稍候...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("upload");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressDialog.dismiss();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload = postsnapshot.getValue(Upload.class);
uploads.add(upload);
}
adapter.setList(uploads);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
适配器部分
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<Upload> uploads;
public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
public RecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
}
public void setList(List<Upload> uploads) {
this.uploads = uploads;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads == null ? 0 : uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}
}
英文:
You have to set the adapter in onCreate
method, right now you're doing it in a listener. My guess is you just need to move the recylerView.setAdapter(adapter)
in onCreate
and just set the datas and notifyDataSetChanged
in your listener
It should look like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview_id);
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
adapter = new RecyclerViewAdapter(getApplicationContext());
recyclerView.setAdapter(adapter)
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
progressDialog.setMessage("Please wait ...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("upload");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressDialog.dismiss();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload = postsnapshot.getValue(Upload.class);
uploads.add(upload);
}
adapter.setList(uploads);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
Adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
private Context mContext;
private List<Upload> uploads;
public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
public RecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
}
public void setList(List<Upload> uploads) {
this.uploads = uploads
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads == null ? 0 : uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论