I am trying to fetch data from database to my recyclerview but error keeps on showing no adapter attached. Below is my code for recyclerview.
XML code with my recycler view
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_comments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="?attr/actionBarSize"/>
My Adapter class is below
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentsHolder>{
private Context context;
private ArrayList<Comment> list;
public CommentAdapter(Context context, ArrayList<Comment> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public CommentsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comment, parent, false);
return new CommentsHolder(v);
}
@Override
public void onBindViewHolder(@NonNull CommentsHolder holder, int position) {
Comment comment = list.get(position);
Picasso.get().load(comment.getUser().getProfile_image_url()).into(holder.circleImageProfileComment);
holder.nameComment.setText(comment.getUser().getName());
holder.dateComment.setText(comment.getDate());
holder.comment.setText(comment.getComment());
}
@Override
public int getItemCount() {
return list.size();
}
class CommentsHolder extends RecyclerView.ViewHolder{
private CircleImageView circleImageProfileComment;
private TextView nameComment, dateComment, comment;
private ImageButton imgBtnComment;
public CommentsHolder(@NonNull View itemView) {
super(itemView);
circleImageProfileComment = itemView.findViewById(R.id.imgCommentProfile);
nameComment = itemView.findViewById(R.id.commentName);
dateComment = itemView.findViewById(R.id.commentDate);
comment = itemView.findViewById(R.id.txtAllComments);
imgBtnComment = itemView.findViewById(R.id.btnCommentOption);
}
}
}
My activity class where i want to display the data code is below
public class CommentActivity extends AppCompatActivity {
private RecyclerView recyclerViewC;
private ArrayList<Comment> list;
private CommentAdapter adapter;
private SharedPreferences sharedPreferences;
private int postId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
init();
}
private void init() {
sharedPreferences = getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
recyclerViewC = findViewById(R.id.recycler_comments);
recyclerViewC.setHasFixedSize(true);
recyclerViewC.setLayoutManager(new LinearLayoutManager(this));
postId = getIntent().getIntExtra("postId", 0);
getComments();
}
private void getComments() {
list = new ArrayList<>();
String token = sharedPreferences.getString("token", "");
AndroidNetworking.post(Constant.COMMENTS)
.addHeaders("Authorization", "Bearer "+token)
.addBodyParameter("postId", postId+"")
.setPriority(Priority.MEDIUM)
.build()
.getAsString(new StringRequestListener() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
if (object.getBoolean("success")) {
JSONArray comments = new JSONArray(object.getString("comments"));
for (int i = 0; i < comments.length(); i++) {
JSONObject comment = comments.getJSONObject(i);
JSONObject user = comment.getJSONObject("user");
User mUser = new User();
mUser.setId(user.getInt("id"));
mUser.setProfile_image_url(user.getString("profile_image_url"));
mUser.setName(user.getString("name"));
Comment mComment = new Comment();
mComment.setId(comment.getInt("id"));
mComment.setUser(mUser);
String dateTime = comment.getString("created_at");
mComment.setDate(dateTime.substring(0,10)+""+dateTime.substring(11,16));
mComment.setComment(comment.getString("comment"));
list.add(mComment);
}
}
adapter = new CommentAdapter(getApplicationContext(), list);
recyclerViewC.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(ANError anError) {
}
});
}
}
I tried to check the adapter class if it was there of which is true. It took alot of hours to debug and changing the code without success
That's not an error--it's a warning that appears if your RecyclerView doesn't have an adapter set on it yet by the time it is drawn. In your case that's because you only create and attach the adapter after your data is available from the network. It's not really a problem, but it might be considered cleaner to create and attach your adapter with empty data in your
init()
method, and update the adapter's data in your networking callback.