I am trying to implement a feature like Reddit that all comments get into the parent comments. It worked, but I don't know why it gives an output of all nested comments in the last comment, which is fetched from the firebase database.
refer to this img for how I am getting output enter image description here
work: I have implemented one recyclerView in mainActivity and another in sample_layout for each item of parent RecyclerView.
My code of parentAdapter is:
public class commentsAdapter extends RecyclerView.Adapter<commentsAdapter.MyHolder> {
ArrayList<commentsModel> list;
ArrayList<commentsModel> listKey;
Context context;
ArrayList<commentsModel> list2 = new ArrayList<>();
ArrayList<commentsModel> listKey2 = new ArrayList<>();
FirebaseDatabase mDatabase;
commentsAdapter adapter;
//
private RecyclerView.RecycledViewPool
viewPool
= new RecyclerView
.RecycledViewPool();
//
public commentsAdapter(ArrayList<commentsModel> list, ArrayList<commentsModel> listKey, Context context) {
this.list = list;
this.listKey = listKey;
this.context = context;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_comment, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
commentsModel adp = list.get(position);
commentsModel adp2 = listKey.get(position);
holder.profileName.setText(adp.getName());
holder.points.setText(adp.getPoints());
holder.postedTime.setText(adp.getTime());
Glide.with(context).load(adp.getImg()).into(holder.profileImg);
//instances
mDatabase = FirebaseDatabase.getInstance();
//Set the inner Recyclerview with adapter
adapter = new commentsAdapter(list2, listKey2, context);
holder.innerRes.setAdapter(adapter);
LinearLayoutManager layoutManager
= new LinearLayoutManager(
holder.innerRes.getContext(),
LinearLayoutManager.HORIZONTAL,
false);
holder.innerRes.setLayoutManager(layoutManager);
holder.innerRes.setHasFixedSize(true);
holder.innerRes.setRecycledViewPool(viewPool);
String path = adp2.getPath();
//Fetch the data from database to populate the recycler view via model
mDatabase.getReference().child(path).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
commentsModel adp = snapshot1.getValue(commentsModel.class);
list2.add(adp);
String path2 = snapshot1.getKey();
String fullPath = path + "/" + path2 + "/Comments";
commentsModel adp2 = new commentsModel(fullPath);
listKey2.add(adp2);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
TextView profileName, postedTime, points;
ShapeableImageView profileImg;
RecyclerView innerRes;
public MyHolder(@NonNull View itemView) {
super(itemView);
profileName = itemView.findViewById(R.id.profileName);
postedTime = itemView.findViewById(R.id.postedTime);
profileImg = itemView.findViewById(R.id.profileImg);
points = itemView.findViewById(R.id.points);
innerRes = itemView.findViewById(R.id.innerRec);
}
}
}
As I told it gives all inner comments in the last comment of topMost parent recyclerView.