How can i pass user id from my adapter to activity - android

i am trying to pass holders user.getID to my attendance activity how can i pass users id from my adapter to attendance activity
this is my attendance activity
reference = FirebaseDatabase.getInstance().getReference("parent");
intent = getIntent();
final String currentSubject = intent.getStringExtra("currentSubject");
reference.addValueEventListener(new ValueEventListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final User user = snapshot.getValue(User.class);
ZoneId zonedId = ZoneId.of("America/Montreal");
final LocalDate today = LocalDate.now(zonedId);
reference = FirebaseDatabase.getInstance().getReference("attendance").child(user.getSchoolcode()).child(grade.getText().toString() + " " + section.getText().toString()).child(currentSubject).child(today.toString()).child(userID);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("attendance", "yes");
reference.updateChildren(hashMap);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
i want to pass it from adapter to this
this is my adapter
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private Context context;
private List<User> users;
public StudentAdapter(Context context, List<User> users) {
this.context = context;
this.users = users;
}
DatabaseReference reference;
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.student_item, parent, false);
return new StudentAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final User user = users.get(position);
holder.studentName.setText(user.getStudentname() + " " + user.getLastname());
}
#Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView studentName;
public Switch aSwitch;
public ViewHolder(View itemView) {
super(itemView);
studentName = itemView.findViewById(R.id.studentName);
aSwitch = itemView.findViewById(R.id.attendanceSwitch);
}
}
this is my adapter which holds the recyclerview,i am trying to get id of users viewed on attendance activities recyclerview

A way that you can use is callback :
In your adapter define an interface like this :
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private SendCallBack mCallback;
interface SendCallBack {
void send(int id);
}
//Initial callback in constructor
public StudentAdapter(Context context, List<User> users,SendCallBack callback) {
this.context = context;
this.users = users;
mCallback = callback;
}
//Now in where that you want to pass data use this
mCallback.send(user.getID);
}
Now in your activity implement the callback like this :
public class YourActivty implements YourAdapter.SendCallback {
#Override
public void send(int id) {
}
//For pass callBack for your adapter constructor use this
}
For other ways you can use this link.

Related

How to compare two values of two different child in firebase database

I want to display the data based on the scheduled date saved on the database, i tried comparing it to the current date of the device, but it didn't work. This is my saved data from firebase:
This is the Logcat
This is the Adapter class:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {
Context context;
ArrayList<Reminder> list;
public ListAdapter(Context context, ArrayList<Reminder> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
Reminder Reminder = list.get(position);
holder.t.setText(Reminder.gett());
holder.d.setText(Reminder.getd());
holder.tm.setText(Reminder.gettm());
holder.dt.setText(Reminder.getdt());
holder.currentDate.setText(Reminder.getcurrentDate());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView t, d, tm, dt, currentDate;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
t = itemView.findViewById(R.id.tvtitle);
d = itemView.findViewById(R.id.tvdesc);
tm = itemView.findViewById(R.id.tvtime);
dt = itemView.findViewById(R.id.tvdate);
currentDate = itemView.findViewById(R.id.tvcdate);
}
}
}
This is my code to retrieve the data from the database:
public class ReminderMainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
Calendar calendar = Calendar.getInstance();
String currentdate = DateFormat.getDateInstance().format(calendar.getTime());
com.example.luntian.adapter.ListAdapter ListAdapter;
ArrayList<Reminder> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder_main);
recyclerView = findViewById(R.id.userList);
database = FirebaseDatabase.getInstance().getReference("Reminder");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<Reminder>();
ListAdapter = new ListAdapter(this,list);
recyclerView.setAdapter(ListAdapter);
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Reminder reminder = dataSnapshot.getValue(Reminder.class);
list.add(reminder);
}
ListAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});

Child recyclerView showing same items for all parent recyclerView (using Firebase Realtime Database)

Basically I want to create a parent recyclerView which shows the categories and the child recyclerView shows the books of that specific category. But the result of my code is giving me the same books for all categories. Below is the necessary code attached.
Java Source Code for home fragment adapter call
private void loadCategories() {
//init arrayList
categoryArrayList = new ArrayList<>();
//get all categories from firebase
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Categories");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
//clear arrayList before adding data to it
categoryArrayList.clear();
for (DataSnapshot ds : snapshot.getChildren()) {
//get data
ModelCategory model = ds.getValue(ModelCategory.class);
//add category to arrayList
categoryArrayList.add(model);
}
//setup adapter
adapterCategoryHome = new AdapterCategoryHome(categoryArrayList);
//set adapter to recyclerView
binding.categoryRv.setAdapter(adapterCategoryHome);
adapterCategoryHome.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
Parent Adapter
public class AdapterCategoryHome extends RecyclerView.Adapter<AdapterCategoryHome.HolderCategory> {
private Context context;
public ArrayList<ModelCategory> categoryArrayList;
public static final String TAG = "CATEGORY_HOME_TAG";
//arrayList to hold data of type ModelPdf
private ArrayList<ModelPdf> pdfArrayList;
private AdapterPdfHome adapterPdfHome;
public AdapterCategoryHome(ArrayList<ModelCategory> categoryArrayList) {
this.categoryArrayList = categoryArrayList;
}
#NonNull
#Override
public HolderCategory onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//bind view of row_category_home.xml
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category_home, parent, false);
this.context = parent.getContext();
return new HolderCategory(view);
}
#Override
public void onBindViewHolder(#NonNull AdapterCategoryHome.HolderCategory holder, int position) {
//get data
ModelCategory model = categoryArrayList.get(position);
String id = model.getId();
String category = model.getCategory();
String uid = model.getUid();
long timestamp = model.getTimestamp();
Log.d(TAG, "onBindViewHolder: "+category);
//set data
holder.categoryTv.setText(category);
loadPdfList(model, holder);
}
private void loadPdfList(ModelCategory model, HolderCategory holder) {
//init list before adding data
pdfArrayList = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Books");
ref.orderByChild("categoryId").equalTo(model.getId())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
pdfArrayList.clear();
for (DataSnapshot ds : snapshot.getChildren()) {
//get data
ModelPdf model = ds.getValue(ModelPdf.class);
//add to list
pdfArrayList.add(model);
}
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
holder.bookRv.setHasFixedSize(true);
Log.d(TAG, "pdf size: " + pdfArrayList.size() + model.getCategory() + holder.categoryTv.getText()+ holder.categoryTv.getContext());
//setup adapter
adapterPdfHome = new AdapterPdfHome(holder.bookRv.getContext(), pdfArrayList);
holder.bookRv.setLayoutManager(layoutManager);
holder.bookRv.setAdapter(adapterPdfHome);
adapterPdfHome.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
#Override
public int getItemCount() {
return categoryArrayList.size();
}
/*View Holder class to hold UI views for row_category.xml*/
class HolderCategory extends RecyclerView.ViewHolder {
//ui views of row_category.xml
TextView categoryTv;
RecyclerView bookRv;
public HolderCategory(#NonNull View itemView) {
super(itemView);
//init ui views
categoryTv = itemView.findViewById(R.id.categoryTv);
bookRv = itemView.findViewById(R.id.bookRv);
}
}
}
Child Adapter
public class AdapterPdfHome extends RecyclerView.Adapter<AdapterPdfHome.HolderPdfAdmin> {
//context
private Context context;
//arrayList to hold list of data of type modelPdf
public ArrayList<ModelPdf> pdfArrayList;
private FilterPdfAdmin filter;
private static final String TAG = "PDF_ADAPTER_TAG";
//progress dialog
private ProgressDialog progressDialog;
//constructor of above
public AdapterPdfHome(Context context, ArrayList<ModelPdf> pdfArrayList) {
this.context = context;
this.pdfArrayList = pdfArrayList;
Log.d(TAG, "AdapterPdfHome: "+pdfArrayList.size());
//init progress dialog
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Please Wait");
progressDialog.setCanceledOnTouchOutside(false);
}
#NonNull
#Override
public HolderPdfAdmin onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//bind view of row_pdf_admin.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_pdf_home,parent,false);
return new HolderPdfAdmin(view);
}
#Override
public void onBindViewHolder(#NonNull AdapterPdfHome.HolderPdfAdmin holder, int position) {
/*Get data, Set Data, handle clicks, etc.*/
//get data
ModelPdf model = pdfArrayList.get(position);
String title = model.getTitle();
String author = model.getAuthor();
Log.d(TAG, "onBindViewHolder: "+title);
//set data
holder.titleTv.setText(title);
holder.authorTv.setText(author);
Glide.with(context)
.load(model.getCoverPageUrl())
.placeholder(R.drawable.back01)
.into(holder.pdfIv);
}
#Override
public int getItemCount() {
//return size of arrayList
return pdfArrayList.size();
}
/*view holder class for row_pdf_user.xml*/
class HolderPdfAdmin extends RecyclerView.ViewHolder {
//UI views of row_pdf_admin.xml
TextView titleTv, authorTv;
ImageView pdfIv;
public HolderPdfAdmin(#NonNull View itemView) {
super(itemView);
//init ui views
titleTv = itemView.findViewById(R.id.titleTv);
authorTv = itemView.findViewById(R.id.authorTv);
pdfIv = itemView.findViewById(R.id.pdfIv);
}
}
}
move "private AdapterPdfHome adapterPdfHome;" to "onBindViewHolder"

get data from specific item in recyclerview according to position passed from firebase

I have created a simple notes app in which I'm storing data in firebase. Showing the list of notes(time and few words of a note) in recyclerview. Now I want when I click in a particular note, it take to different activity where the details of the note will be shown. I am not able to get data from firebase column wise(note and time).
CustomAdapter
Here I'm sending the position of the item clicked to NotesDetails class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
Context context;
ArrayList<Model> models;
public CustomAdapter(Context context, ArrayList<Model> models){
this.context = context;
this.models = models;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_content, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.time_text.setText(models.get(holder.getBindingAdapterPosition()).getText());
holder.string_text.setText(models.get(holder.getBindingAdapterPosition()).getNotes());
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context.getApplicationContext(), NotesDetails.class);
intent.putExtra("position", String.valueOf(holder.getBindingAdapterPosition()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() { return models.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView string_text, time_text;
ConstraintLayout constraintLayout;
public MyViewHolder(View itemView) {
super(itemView);
string_text = itemView.findViewById(R.id.textView);
time_text = itemView.findViewById(R.id.timeshow);
constraintLayout = itemView.findViewById(R.id.constraintLayout);
}
}
}
NotesDetails
I want to show time and details of particular item here in testShow and timeShow
public class NotesDetails extends AppCompatActivity {
ArrayList<Model> models;
TextView textShow, timeShow;
DBHelper myDB;
DatabaseReference dbReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_details);
textShow = findViewById(R.id.notesShow);
timeShow = findViewById(R.id.timeshow);
myDB = new DBHelper(this);
Intent intent = getIntent();
int position = Integer.parseInt(intent.getStringExtra("position"));
dbReference = FirebaseDatabase.getInstance().getReference("Model").child("notes");
dbReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot: snapshot.getChildren()){
Model m1 = dataSnapshot.getValue(Model.class);
textShow.setText((CharSequence) m1);
}
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
}
});
}
}

How do I show info from two different models / adapters in one recyclerview

I have two Models (ModelChat and ModelUsers) and their class adapters (PostsAdapter and AllUsersAdapter).
I want to get and set user info such as name and email from the ModelUser and get and set post information from the ModelPost from Firebase database into the same recyclerview in my HomeActivity.
The codes are as shown below:
PostsAdapter.java
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.MyHolder>{
Context context;
List<ModelPost> postList;
public PostsAdapter(Context context, List<ModelPost> postList) {
this.context = context;
this.postList = postList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//inflate layout row_post.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_posts, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = postList.get(position).getUserUID();
String userEmail = postList.get(position).getUserEmail();
String userProfilePic = postList.get(position).getUserProfilePic();
String firstName = postList.get(position).getUserFirstName();
String lastName = postList.get(position).getUserLastName();
String userNickName = postList.get(position).getUserNickName();
String postTimeStamp = postList.get(position).getPostTime();
String userPost = postList.get(position).getUserPost();
//set data
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.avatarTv);
holder.firstnameTv.setText(firstName);
holder.lastnameTv.setText(lastName);
holder.nicknameTv.setText(userNickName);
}
#Override
public int getItemCount() {
return postList.size();
}
//view holder class
class MyHolder extends RecyclerView.ViewHolder {
//views from row_posts layout
public MyHolder(#NonNull View itemView) {
super(itemView);
//init view
//my views here
}
}
}
AllUsersAdapter.java
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.MyHolder>{
Context context;
List<ModelUser> userList;
//Constructor > Generate
public AllUsersAdapter(Context context, List<ModelUser> userList) {
this.context = context;
this.userList = userList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflate layout (row_user)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_all_users, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = userList.get(position).getUid();
String userProfilePic = userList.get(position).getProfilepic();
String userName = userList.get(position).getFirstname() + " " + userList.get(position).getLastname();
String userNickname = userList.get(position).getNickname();
String userStatus = userList.get(position).getStatus();
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.mAvatarIv);
holder.mNameTv.setText(userName);
holder.mNicknameTv.setText(userNickname);
holder.mStatusTv.setText(userStatus);
//Handle item click
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*Click user from user list to start conversation
*Start Activity by putting UID of receiver
*we will use that UID to identify the user we are gonna chat */
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userUid", userUID);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return userList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
public MyHolder(#NonNull View itemView) {
super(itemView);
//init my views here
}
}
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
FirebaseAuth firebaseAuth;
RecyclerView recyclerView;
List<ModelPost> postList;
PostsAdapter postsAdapter;
String mUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseAuth = FirebaseAuth.getInstance();
//recyclerview and its properties
recyclerView = findViewById(R.id.posts_recyclerview);
//init post list
postList = new ArrayList<>();
loadPosts();
}
private void loadPosts() {
//path of all posts
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
//get all data from this reference
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
postList.clear();
for (DataSnapshot ds: snapshot.getChildren()) {
ModelPost modelPost = ds.getValue(ModelPost.class);
postList.add(modelPost);
//adapter
postsAdapter = new PostsAdapter(HomeActivity.this, postList);
//set adapter to recyclerview
recyclerView.setAdapter(postsAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
//in case of errors
//Toast.makeText(HomeActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try using ConcatAdapter.
An RecyclerView.Adapter implementation that presents the contents of multiple adapters in sequence. More info
Concatenate adapters sequentially with ConcatAdapter:
https://medium.com/androiddevelopers/merge-adapters-sequentially-with-mergeadapter-294d2942127a
I think this will help
MyAdapter adapter1 = ...;
AnotherAdapter adapter2 = ...;
ConcatAdapter concatenated = new ConcatAdapter(adapter1, adapter2);
recyclerView.setAdapter(concatenated);

How to handle RecyclerView positions in a correct way?

I am using a recyclerview and inside its adapter I have another recyclerview.
How to prevent item change and item refresh when an item is updated.
This is creating visual glitches and wrong positioning of the items
I have tried to read directly from a database reference on the current position of the holder.
Result: https://media.giphy.com/media/j6TpXZRE6e44cMZVIP/giphy.gif
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder>{
private Context mContext;
private List<Comments> mComments;
private List<Replies> allReplies;
public CommentsAdapter(Context mContext, List<Comments> mComments, List<Replies> allReplies )
{
this.mContext = mContext;
this.mComments = mComments;
this.allReplies = allReplies;
}
#NonNull
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.all_comments_layout, viewGroup, false);
return new CommentsAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position)
{
final Comments comments = mComments.get(position);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentUps.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
{
if (dataSnapshot.child(PostKey).child(com_uid).hasChild(currentUserID)) {
CommentUps.child(PostKey).child(com_uid).child(currentUserID).removeValue();
CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() - 1);
} else {
CommentUps.child(PostKey).child(com_uid).child(currentUserID).setValue(true);
CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() + 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");
//reference.orderByChild("pLikes")
reference.orderByChild(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
holder.repliesList.clear();
for (DataSnapshot snapshot : dataSnapshot.child(PostKey).child(com_uid).getChildren())
{
Replies replies = snapshot.getValue(Replies.class);
//final String com = comment.toString();
// Log.d(new String("Comments"), com);
//for (String id : followingList)
if (replies.getComment_uid().equals(com_uid))
{
holder.repliesList.add(replies);
}
}
holder.repliesAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public int getItemCount() {
return mComments.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageButton like
List<Replies> repliesList;
RepliesAdapter repliesAdapter;
public ViewHolder(View itemView) {
super(itemView);
like = itemView.findViewById(R.id.comment_like_button);
repliesRecycler = itemView.findViewById(R.id.all_replies_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
repliesRecycler.setLayoutManager(linearLayoutManager);
repliesRecycler.setHasFixedSize(true);
repliesList = new ArrayList<>();
repliesAdapter = new RepliesAdapter(mContext, repliesList);
repliesRecycler.setAdapter(repliesAdapter);
repliesAdapter.notifyDataSetChanged();
}
// Second Method - Same Result
private void displayReplies(final String com_uid ,final String PostKey, final boolean locked) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");
reference.child(PostKey).child(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
repliesList.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final Replies replies = snapshot.getValue(Replies.class);
if (replies.getComment_uid().equals(com_uid)) {
repliesList.add(replies);
repliesAdapter.notifyDataSetChanged();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
My replies adapter:
public class RepliesAdapter extends RecyclerView.Adapter<RepliesAdapter.ViewHolder>
{
public Context mContext;
public List<Replies> mReplies;
public RepliesAdapter(Context mContext, List<Replies> mReplies)
{
this.mContext = mContext;
this.mReplies = mReplies;
}
#NonNull
#Override
public RepliesAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.all_replies_layout, viewGroup, false);
mAuth = FirebaseAuth.getInstance();
return new RepliesAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position)
{
final Replies replies = mReplies.get(position);
holder.myUserName.setText(replies.getUsername());
holder.myComment.setText(replies.getReply());
holder.myTime.setText(replies.getTime());
}
#Override
public int getItemCount() {
return mReplies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView myUserName, myComment,myTime;
public ViewHolder(View itemView) {
super(itemView);
myUserName = itemView.findViewById(R.id.reply_username);
myComment = itemView.findViewById(R.id.reply_text);
myTime = itemView.findViewById(R.id.reply_time);
}
}

Categories

Resources