Home > Enterprise >  Android: No adapter attach,
Android: No adapter attach,

Time:10-10

I am creating a listview in tabLayout. When I run the app in it says no adapter attach, skipping layout and I cannot see list in emulator; it appears blank. Please help me. I tried almost every answer on internet but could not make it. I can see all other things except list ion the screen.

''' 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<>();
}

}

'''

CodePudding user response:

You are getting this warning because you haven't set adapter to recycler view in onCreate(). you have set adapter to recycler view inside addValueEventListener that why adapter only attaching to recycler view when onDataChange method triggers.

    recyclerView = findViewById(R.id.recyclerview);
    recyclerAdapter=new RecyclerAdapter()        //Initialize adapter
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
   recyclerView.setAdapter(recyclerAdapter);        //set adapter
    recyclerView.setHasFixedSize(true);

CodePudding user response:

first initialize the adapter with emptyList (bookList) in onCreate(), and then when onDataChange() is called update bookList and call recyclerAdapter.notifyDataSetChanged() as you have already done.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recyclerview);
    booklist = new ArrayList<Book>();
    recyclerAdapter = new RecyclerAdapter(getApplicationContext(), bookList);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(recyclerAdapter);
    recyclerView.setHasFixedSize(true);
  • Related