Tengo el problema E/RecyclerView: No adapter attached; skipping layout no estoy muy seguro por qué pasa ya que creo que tengo todo conectado correctamente. el problema que está ocurriendo es que el recycler aparece completamente vacío, estoy utilizando firebase pero no creo que el problema tenga que ver con eso ya que puedo subir información y descargarla, simplemente es el recyclerView que por alguna razón no está funcionando correctamente
Aquí está el código
public class PostListActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private FirebaseDatabase mDatabase;
private RecyclerView recyclerView;
private FirebaseUser mUser;
private FirebaseAuth mAuth;
private BlogRecyclerAdapter blogRecyclerAdapter;
private List<Blog> blogList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_list);
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mDatabase.getReference().child("MBlog");
mDatabaseReference.keepSynced(true);
blogList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Toast.makeText(PostListActivity.this, "Blog", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_Add:
if(mUser != null && mAuth != null){
startActivity(new Intent(PostListActivity.this, AddPostActivity.class));
finish();
}
break;
case R.id.action_signout:
if(mUser != null && mAuth != null){
mAuth.signOut();
startActivity(new Intent(PostListActivity.this, MainActivity.class));
finish();
}
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
mDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Blog blog = dataSnapshot.getValue(Blog.class);
blogList.add(blog);
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
blogRecyclerAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
y el recycler. no entiendo por qué me aparece este error ya que considero que todo está bien.
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder>{
private Context context;
private List<Blog> blogList;
public BlogRecyclerAdapter(Context context, List<Blog> blogList) {
this.context = context;
this.blogList = blogList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row, parent, false);
return new ViewHolder(view, context);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Blog blog = blogList.get(position);
String imagerUrl = null;
holder.title.setText(blog.getTitle());
holder.desc.setText(blog.getDesc());
java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance();
String formattedDate = dateFormat.format(new Date(Long.valueOf(blog.getTimestamp())).getTime());
holder.timestamp.setText(formattedDate);
imagerUrl = blog.getImage();
//TODO: Use picassoo library to load image
}
@Override
public int getItemCount() {
return blogList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView title;
public TextView desc;
public TextView timestamp;
public ImageView image;
String userId;
public ViewHolder(@NonNull View view, Context ctx) {
super(view);
context = ctx;
title = (TextView) view.findViewById(R.id.postTitleList);
desc = (TextView) view.findViewById(R.id.postTextList);
image = (ImageView) view.findViewById(R.id.postImageList);
timestamp = (TextView) view.findViewById(R.id.timeStampList);
userId = null;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//go to next activity
}
});
}
}
}
Muchas gracias por la ayuda.
onChildAdded
el objetoblogList
se está llenando con datos? No veo que hagas ahí ningún control al respecto. Generalmente ese error ocurre cuando el adaptar se llena con datos vacíos.