This is my HomeView
Fragment Activity
public class HomeView extends Fragment {
private View view;
private RecyclerView recentPosts;
private DatabaseReference donor_ref;
FirebaseAuth mAuth;
private BloodRequestAdapter restAdapter;
private List<CustomUserData> postLists;
private ProgressDialog pd;
public HomeView() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_view_fragment, container, false);
recentPosts = (RecyclerView) view.findViewById(R.id.recyleposts);
recentPosts.setLayoutManager(new LinearLayoutManager(getContext()));
donor_ref = FirebaseDatabase.getInstance().getReference();
postLists = new ArrayList<>();
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.setCancelable(true);
pd.setCanceledOnTouchOutside(false);
mAuth = FirebaseAuth.getInstance();
getActivity().setTitle("Blood Lak");
restAdapter = new BloodRequestAdapter(postLists);
RecyclerView.LayoutManager pmLayout = new LinearLayoutManager(getContext());
recentPosts.setLayoutManager(pmLayout);
recentPosts.setItemAnimator(new DefaultItemAnimator());
recentPosts.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recentPosts.setAdapter(restAdapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recentPosts.setLayoutManager(mLayoutManager);
AddPosts();
return view;
}
private void AddPosts()
{
Query allposts = donor_ref.child("posts");
pd.show();
allposts.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
for (DataSnapshot singlepost : dataSnapshot.getChildren()) {
CustomUserData customUserData = singlepost.getValue(CustomUserData.class);
postLists.add(customUserData);
restAdapter.notifyDataSetChanged();
}
pd.dismiss();
}
else
{
Toast.makeText(getActivity(), "No Blood Request Found!",
Toast.LENGTH_LONG).show();
}
pd.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
This is my RecyclerView
Adapter. I need to sort user posts into date-wise in recycler view in Android Studio, which means newest to oldest based on date and time. I created a database using Firebase. My present problem is I sorted posts using the ReverseLayout(true) function but some posts are not sorted from newest to oldest because of user submitted a post to the Firebase database posts nodes are shuffled when the user submits a new post, not the newest to oldest this issue still in firebase database.
public class BloodRequestAdapter extends RecyclerView.Adapter<BloodRequestAdapter.PostHolder> {
private List<CustomUserData> postLists;
public class PostHolder extends RecyclerView.ViewHolder {
TextView Name, bloodgroup, Address, contact, posted;
public PostHolder(@NonNull View itemView) {
super(itemView);
Name = itemView.findViewById(R.id.reqstUser);
contact = itemView.findViewById(R.id.targetCN);
bloodgroup = itemView.findViewById(R.id.targetBG);
Address = itemView.findViewById(R.id.reqstLocation);
posted = itemView.findViewById(R.id.posted);
}
}
public BloodRequestAdapter(List<CustomUserData> postLists)
{
this.postLists = postLists;
}
@Override
public PostHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View listitem = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.request_list_item, viewGroup, false);
return new PostHolder(listitem);
}
@Override
public void onBindViewHolder(@NonNull PostHolder postHolder, final int i ) {
if(i%2==0)
{
postHolder.itemView.setBackgroundColor(Color.parseColor("#C13F31"));
}
else
{
postHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
/***postHolder.delete.setOnClickListener(new View.OnClickListener() {
String uid;
FirebaseAuth mAuth;
@Override
public void onClick(View v) {
mAuth = FirebaseAuth.getInstance();
FirebaseUser cur_user = FirebaseAuth.getInstance().getCurrentUser();
uid = cur_user.getUid();
FirebaseDatabase.getInstance().getReference()
.child("posts")
.child(mAuth.getCurrentUser().getUid())
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
}); ***/
CustomUserData customUserData = postLists.get(i);
postHolder.Name.setText("Posted by: "+customUserData.getName());
postHolder.Address.setText("To: "+customUserData.getAddress()+", "+customUserData.getDivision());
postHolder.bloodgroup.setText("Needs "+customUserData.getBloodGroup());
postHolder.posted.setText("Posted on:"+customUserData.getTime()+", "+customUserData.getDate());
postHolder.contact.setText(customUserData.getContact());
}
@Override
public int getItemCount() {
return postLists.size();
}
}
This is my CustomeUserData Class
public class CustomUserData implements Serializable {
private String Address, Division, Contact;
private String Name, BloodGroup;
private String Time, Date;
public CustomUserData() {
}
public CustomUserData(String address, String division, String contact, String name, String bloodGroup, String time, String date) {
Address = address;
Division = division;
Contact = contact;
Name = name;
BloodGroup = bloodGroup;
Time = time;
Date = date;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.Address = address;
}
public String getDivision() {
return Division;
}
public void setDivision(String division) {
this.Division = division;
}
public String getContact() {
return Contact;
}
public void setContact(String contact) {
this.Contact = contact;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getBloodGroup() {
return BloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.BloodGroup = bloodGroup;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
this.Time = time;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
this.Date = date;
}
}
This is my App side, i have used this methord called .setReverseLayout(true); mLayoutManager.setStackFromEnd(true);
not working
Check my app side and firebase database
CustomUserData
class.Date
field?