Unable to get the Cardview, E/RecyclerView: No adapter attached; skipping layout error , how to add adapter to recyclerView?

huangapple go评论138阅读模式
英文:

Unable to get the Cardview, E/RecyclerView: No adapter attached; skipping layout error , how to add adapter to recyclerView?

问题

以下是你提供的代码的中文翻译:

  1. 我正在使用Android Studio构建一个应用程序在运行应用程序时我遇到了E/RecyclerView: No adapter attached; skipping layout的问题
  2. 由于我对Android Studio开发还很新无法找出问题所在我尝试了一些在线解决方案但对我没有起作用
  3. public class MainActivity extends AppCompatActivity {
  4. RecyclerView recyclerView;
  5. RecyclerViewAdapter adapter;
  6. DatabaseReference mDatabase;
  7. ProgressDialog progressDialog;
  8. private List<Upload> uploads;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. recyclerView = findViewById(R.id.recyclerview_id);
  14. recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
  15. progressDialog = new ProgressDialog(this);
  16. uploads = new ArrayList<>();
  17. progressDialog.setMessage("请稍候...");
  18. progressDialog.show();
  19. mDatabase = FirebaseDatabase.getInstance().getReference("upload");
  20. mDatabase.addValueEventListener(new ValueEventListener() {
  21. @Override
  22. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  23. progressDialog.dismiss();
  24. for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
  25. Upload upload = postsnapshot.getValue(Upload.class);
  26. uploads.add(upload);
  27. }
  28. adapter = new RecyclerViewAdapter(getApplicationContext(), uploads);
  29. recyclerView.setAdapter(adapter);
  30. adapter.notifyDataSetChanged();
  31. }
  32. @Override
  33. public void onCancelled(@NonNull DatabaseError databaseError) {
  34. progressDialog.dismiss();
  35. }
  36. });
  37. }
  38. }
  39. **适配器**
  40. 这是我的适配器代码有时候会出错或者我可能犯了一个错误请检查这两个地方并给我一个纠正这个问题的想法
  41. public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
  42. private Context mContext;
  43. private List<Upload> uploads;
  44. public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
  45. this.mContext = mContext;
  46. this.uploads = uploads;
  47. }
  48. @NonNull
  49. @Override
  50. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  51. View view;
  52. LayoutInflater inflater = LayoutInflater.from(mContext);
  53. view = inflater.inflate(R.layout.card_view_item, parent, false);
  54. return new MyViewHolder(view);
  55. }
  56. @Override
  57. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  58. final Upload upload = uploads.get(position);
  59. holder.tv_book_title.setText(upload.getName());
  60. Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
  61. }
  62. @Override
  63. public int getItemCount() {
  64. return uploads.size();
  65. }
  66. public class MyViewHolder extends RecyclerView.ViewHolder {
  67. TextView tv_book_title;
  68. ImageView imd_book_thumbnail;
  69. CardView cardView;
  70. public MyViewHolder(@NonNull View itemView) {
  71. super(itemView);
  72. tv_book_title = itemView.findViewById(R.id.book_title_id);
  73. imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
  74. cardView = itemView.findViewById(R.id.card_view_id);
  75. }
  76. }
  77. }
英文:

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.

  1. public class MainActivity extends AppCompatActivity {
  2. RecyclerView recyclerView;
  3. RecyclerViewAdapter adapter;
  4. DatabaseReference mDatabase;
  5. ProgressDialog progressDialog;
  6. private List &lt;Upload&gt; uploads;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. recyclerView = findViewById(R.id.recyclerview_id);
  12. recyclerView.setLayoutManager(new GridLayoutManager(this,3));
  13. progressDialog = new ProgressDialog(this);
  14. uploads = new ArrayList&lt;&gt;();
  15. progressDialog.setMessage(&quot;Please wait ...&quot;);
  16. progressDialog.show();
  17. mDatabase = FirebaseDatabase.getInstance().getReference(&quot;upload&quot;);
  18. mDatabase.addValueEventListener(new ValueEventListener() {
  19. @Override
  20. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  21. progressDialog.dismiss();
  22. for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
  23. Upload upload = postsnapshot.getValue(Upload.class);
  24. uploads.add(upload);
  25. }
  26. adapter = new RecyclerViewAdapter(getApplicationContext(),uploads);
  27. recyclerView.setAdapter(adapter);
  28. adapter.notifyDataSetChanged();
  29. }
  30. @Override
  31. public void onCancelled(@NonNull DatabaseError databaseError) {
  32. progressDialog.dismiss();
  33. }
  34. });
  35. }

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.

  1. public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;{
  2. private Context mContext;
  3. private List &lt;Upload&gt; uploads;
  4. public RecyclerViewAdapter(Context mContext, List&lt;Upload&gt; uploads) {
  5. this.mContext = mContext;
  6. this.uploads = uploads;
  7. }
  8. @NonNull
  9. @Override
  10. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  11. View view;
  12. LayoutInflater inflater = LayoutInflater.from(mContext);
  13. view = inflater.inflate(R.layout.card_view_item,parent,false);
  14. return new MyViewHolder(view);
  15. }
  16. @Override
  17. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  18. final Upload upload = uploads.get(position);
  19. holder.tv_book_title.setText(upload.getName());
  20. Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
  21. }
  22. @Override
  23. public int getItemCount() {
  24. return uploads.size();
  25. }
  26. public class MyViewHolder extends RecyclerView.ViewHolder {
  27. TextView tv_book_title;
  28. ImageView imd_book_thumbnail;
  29. CardView cardView;
  30. public MyViewHolder(@NonNull View itemView) {
  31. super(itemView);
  32. tv_book_title = itemView.findViewById(R.id.book_title_id);
  33. imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
  34. cardView = itemView.findViewById(R.id.card_view_id);
  35. }
  36. }

}

答案1

得分: 1

你需要在 onCreate 方法中设置适配器,目前你是在监听器中设置的。我猜想你只需要将 recylerView.setAdapter(adapter) 移动到 onCreate 中,然后在你的监听器中设置数据和调用 notifyDataSetChanged

应该是这样的:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. recyclerView = findViewById(R.id.recyclerview_id);
  6. recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
  7. adapter = new RecyclerViewAdapter(getApplicationContext());
  8. recyclerView.setAdapter(adapter);
  9. progressDialog = new ProgressDialog(this);
  10. uploads = new ArrayList<>();
  11. progressDialog.setMessage("请稍候...");
  12. progressDialog.show();
  13. mDatabase = FirebaseDatabase.getInstance().getReference("upload");
  14. mDatabase.addValueEventListener(new ValueEventListener() {
  15. @Override
  16. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  17. progressDialog.dismiss();
  18. for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
  19. Upload upload = postsnapshot.getValue(Upload.class);
  20. uploads.add(upload);
  21. }
  22. adapter.setList(uploads);
  23. }
  24. @Override
  25. public void onCancelled(@NonNull DatabaseError databaseError) {
  26. progressDialog.dismiss();
  27. }
  28. });
  29. }

适配器部分

  1. public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
  2. private Context mContext;
  3. private List<Upload> uploads;
  4. public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
  5. this.mContext = mContext;
  6. this.uploads = uploads;
  7. }
  8. public RecyclerViewAdapter(Context mContext) {
  9. this.mContext = mContext;
  10. }
  11. public void setList(List<Upload> uploads) {
  12. this.uploads = uploads;
  13. }
  14. @NonNull
  15. @Override
  16. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  17. View view;
  18. LayoutInflater inflater = LayoutInflater.from(mContext);
  19. view = inflater.inflate(R.layout.card_view_item, parent, false);
  20. return new MyViewHolder(view);
  21. }
  22. @Override
  23. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  24. final Upload upload = uploads.get(position);
  25. holder.tv_book_title.setText(upload.getName());
  26. Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
  27. }
  28. @Override
  29. public int getItemCount() {
  30. return uploads == null ? 0 : uploads.size();
  31. }
  32. public class MyViewHolder extends RecyclerView.ViewHolder {
  33. TextView tv_book_title;
  34. ImageView imd_book_thumbnail;
  35. CardView cardView;
  36. public MyViewHolder(@NonNull View itemView) {
  37. super(itemView);
  38. tv_book_title = itemView.findViewById(R.id.book_title_id);
  39. imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
  40. cardView = itemView.findViewById(R.id.card_view_id);
  41. }
  42. }
  43. }
英文:

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:

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. recyclerView = findViewById(R.id.recyclerview_id);
  5. recyclerView.setLayoutManager(new GridLayoutManager(this,3));
  6. adapter = new RecyclerViewAdapter(getApplicationContext());
  7. recyclerView.setAdapter(adapter)
  8. progressDialog = new ProgressDialog(this);
  9. uploads = new ArrayList&lt;&gt;();
  10. progressDialog.setMessage(&quot;Please wait ...&quot;);
  11. progressDialog.show();
  12. mDatabase = FirebaseDatabase.getInstance().getReference(&quot;upload&quot;);
  13. mDatabase.addValueEventListener(new ValueEventListener() {
  14. @Override
  15. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  16. progressDialog.dismiss();
  17. for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
  18. Upload upload = postsnapshot.getValue(Upload.class);
  19. uploads.add(upload);
  20. }
  21. adapter.setList(uploads);
  22. }
  23. @Override
  24. public void onCancelled(@NonNull DatabaseError databaseError) {
  25. progressDialog.dismiss();
  26. }
  27. });
  28. }

Adapter

  1. public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;{
  2. private Context mContext;
  3. private List&lt;Upload&gt; uploads;
  4. public RecyclerViewAdapter(Context mContext, List&lt;Upload&gt; uploads) {
  5. this.mContext = mContext;
  6. this.uploads = uploads;
  7. }
  8. public RecyclerViewAdapter(Context mContext) {
  9. this.mContext = mContext;
  10. }
  11. public void setList(List&lt;Upload&gt; uploads) {
  12. this.uploads = uploads
  13. }
  14. @NonNull
  15. @Override
  16. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  17. View view;
  18. LayoutInflater inflater = LayoutInflater.from(mContext);
  19. view = inflater.inflate(R.layout.card_view_item,parent,false);
  20. return new MyViewHolder(view);
  21. }
  22. @Override
  23. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  24. final Upload upload = uploads.get(position);
  25. holder.tv_book_title.setText(upload.getName());
  26. Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
  27. }
  28. @Override
  29. public int getItemCount() {
  30. return uploads == null ? 0 : uploads.size();
  31. }
  32. public class MyViewHolder extends RecyclerView.ViewHolder {
  33. TextView tv_book_title;
  34. ImageView imd_book_thumbnail;
  35. CardView cardView;
  36. public MyViewHolder(@NonNull View itemView) {
  37. super(itemView);
  38. tv_book_title = itemView.findViewById(R.id.book_title_id);
  39. imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
  40. cardView = itemView.findViewById(R.id.card_view_id);
  41. }
  42. }

huangapple
  • 本文由 发表于 2020年5月3日 17:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定