[英]No Adapter Attached Skipping layout error with RecyclerView Adapter
These are steps I am doing in this project:这些是我在这个项目中做的步骤:
The ArrayList Activity file: ArrayList 活动文件:
Initializing the recyclerView in OnCreate在 OnCreate 中初始化 recyclerView
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_journal_list); firebaseAuth= FirebaseAuth.getInstance(); user= firebaseAuth.getCurrentUser(); noJournalEntry= findViewById(R.id.list_no_thoughts); journalList= new ArrayList<>(); recyclerView= (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setHasFixedSize(true); }
Invoking the RecyclerView in onStart:在 onStart 中调用 RecyclerView:
@Override protected void onStart() { super.onStart(); //we are getting the user Id of the person who is logged in collectionReference.whereEqualTo("userId", JournalApi.getInstance() .getUserId()) .get() .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { @Override public void onSuccess(QuerySnapshot queryDocumentSnapshots) { if(!queryDocumentSnapshots.isEmpty()) { for(QueryDocumentSnapshot journals: queryDocumentSnapshots){ Journal journal = journals.toObject(Journal.class); journalList.add(journal); Toast.makeText(JournalListActivity.this,"UserId found", Toast.LENGTH_LONG).show(); } // Invoke Recyclerview journalRecyclerAdapter= new JournalRecyclerAdapter(JournalListActivity.this, journalList); recyclerView.setAdapter(journalRecyclerAdapter); journalRecyclerAdapter.notifyDataSetChanged(); } else { noJournalEntry.setVisibility(View.VISIBLE); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(JournalListActivity.this,"UserId NOT found", Toast.LENGTH_LONG).show(); } }); }
} }
As you can see that the RecyclerView is called fine, but the ArrayList is not loading.正如您所看到的, RecyclerView 被调用得很好,但 ArrayList 没有加载。
The Error : E/RecyclerView: No adapter attached; skipping layout
错误:
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
I referred to various posts in Stackoverflow and followed the steps, not sure where I am going wrong.我参考了 Stackoverflow 中的各种帖子并按照步骤操作,不确定我哪里出错了。
You are getting the following warning, not an error:您收到以下警告,而不是错误:
E/RecyclerView: No adapter attached;
E/RecyclerView:没有附加适配器; skipping layout
跳过布局
Because you are setting the adapter in a background thread.因为您是在后台线程中设置适配器。 To solve this, you have to set the adapter outside the callback and inside it, just notify it about the changes.
要解决此问题,您必须在回调外部和内部设置适配器,只需将更改通知它即可。 So please move the following lines of code:
因此,请移动以下代码行:
journalRecyclerAdapter= new JournalRecyclerAdapter(JournalListActivity.this, journalist);
recyclerView.setAdapter(journalRecyclerAdapter);
Right after:紧随其后:
super.onStart();
And leave the following line as it is inside the callback:并将以下行保留在回调中:
journalRecyclerAdapter.notifyDataSetChanged();
And the warning will disapear.并且警告将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.