[英]No Adapter Attached Skipping layout error with RecyclerView Adapter
这些是我在这个项目中做的步骤:
ArrayList 活动文件:
在 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); }
在 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(); } }); }
}
正如您所看到的, RecyclerView 被调用得很好,但 ArrayList 没有加载。
错误: E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
我参考了 Stackoverflow 中的各种帖子并按照步骤操作,不确定我哪里出错了。
您收到以下警告,而不是错误:
E/RecyclerView:没有附加适配器; 跳过布局
因为您是在后台线程中设置适配器。 要解决此问题,您必须在回调外部和内部设置适配器,只需将更改通知它即可。 因此,请移动以下代码行:
journalRecyclerAdapter= new JournalRecyclerAdapter(JournalListActivity.this, journalist);
recyclerView.setAdapter(journalRecyclerAdapter);
紧随其后:
super.onStart();
并将以下行保留在回调中:
journalRecyclerAdapter.notifyDataSetChanged();
并且警告将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.