【发布时间】:2021-11-29 02:16:08
【问题描述】:
我正在 tabLayout 中创建一个列表视图。当我在其中运行应用程序时,它说没有适配器附加,跳过布局,我在模拟器中看不到列表;它显示为空白。请帮我。我几乎尝试了互联网上的所有答案,但无法做到。
除了列出屏幕之外,我可以看到所有其他内容。
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private DatabaseReference myRef;
private ArrayList<Book> bookList;
private RecyclerAdapter recyclerAdapter;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
myRef = FirebaseDatabase.getInstance().getReference();
bookList = new ArrayList<>();
ClearAll();
GetDataFromFirebase();
}
private void GetDataFromFirebase() {
Query query = myRef.child("Book");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
ClearAll();
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
Book book = new Book();
book.setImage(snapshot.child("image").getValue().toString());
book.setTitle(snapshot.child("Title").getValue().toString());
book.setCat(snapshot.child("Cat").getValue().toString());
bookList.add(book);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), bookList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void ClearAll(){
if (bookList != null){
bookList.clear();
if (recyclerAdapter != null){
recyclerAdapter.notifyDataSetChanged();
}
}
bookList = new ArrayList<>();
}
}
【问题讨论】:
标签: java android android-listview