Reputation: 23
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
Upvotes: 0
Views: 81
Reputation: 138969
If you want to have your Firebase Realtime Database query results arranged in a certain way, then you should either order the results by key, by value, or by a specific child.
If you order the results by the keys, it:
Creates a query in which child nodes are ordered by their keys.
So in such a case, the best option that you have is to use the push() method which:
Create a reference to an auto-generated child location.
The natural order of the results is always ascending. So if this is not what you want, and you need to arrange the results descending, then I recommend you reverse the layout as explained in my answer from the following post:
When it comes to orderByValue()
, I think that everything is straightforward. However, what I think you're looking for is the option to order the results by a specific date
child(field). As I see in your screenshot, you already have such a child but there are two issues:
The fields in the class and in the database start with an uppercase letter which will not work as expected. However, here is the fix. The solution in the linked answer is for Firestore, but the same solution applies in the case of the Realtime Database.
You saved the date as strings. When you order string elements, the order that you get is always lexicographically. This means that strings don't consider any sort of numeric values when sorting, especially when it comes to the dates, even if the dates contain numbers, as seen in your screenshot. So in this case, the best option that you have is to store the date and time as a timestamp. So if you need to reverse the order, meaning that all your objects have to be displayed in your RecyclerView descending, then besides reversing the layout, you might consider storing an inverted Timestamp value, as explained in my answer from the following post:
Here is also an example of some Java code that can help you achieve what you want:
Upvotes: 1