Reputation: 49
I'm facing this issue with my Recyclerview. I'm trying to display images from another firebase database that I have. It was working fine before and now I get two blank card view and no image when I run my app. I am not sure what is going on because it worked fine before. Can someone please help me with this issue. Thanks in advance. My code below
//Code
recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),1);
recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<Model>();
aAdapter = new Adapter(MainActivity2.this, myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
progressBar=findViewById(R.id.progressBar);
databaseReference = FirebaseDatabase.getInstance("https://puttt0-4331.firebaseio.com").getReference("Images");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
progressBar.setVisibility(View.GONE);
if(snapshot.exists()){
for (DataSnapshot postsnapshot : snapshot.getChildren()) {
Model upload=postsnapshot.getValue(Model.class);
//myUploads.clear();
myUploads.add(upload);
aAdapter = new Adapter(MainActivity2.this, myUploads);
recyclerView.setAdapter(aAdapter);
//aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}
aAdapter.notifyDataSetChanged();
Upvotes: 1
Views: 62
Reputation: 2066
You should not
setAdapter(newAdapter)
each time you get new data from the OnDataChange() method
here is what you should do:
if(snapshot.exists()){
myUploads.clear();
for (DataSnapshot postsnapshot : snapshot.getChildren()) {
Model upload=postsnapshot.getValue(Model.class);
myUploads.add(upload);
}
aAdapter.notifyDataSetChanged();
}
Try this and tell me what happens. And always remember you have to have ONLY ONE initialization for the adapter, i.e.
aAdapter = new Adapter(MainActivity2.this, myUploads);//this should only be done only once for the adapter
Upvotes: 1