Search code examples
androidfirebasefirebase-realtime-databaseandroid-recyclerviewfirebaseui

How do I arrange items in Firebase Recycler View according to the proximity to client's current time


Tab 2 needs to show the items in decreasing proximity to client's Current time and date,reminderTime and reminderDate are feeded in already in the Firebase Database,What's the best way to get the items in the recycler view in order of decreasing proximity from clients time and date.(Nearestcomes on top).Any help will be appreciated.

Here is my Firebase Database

Tab 2 Code

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            //Returning the layout file after inflating
            //Change R.layout.tab1 in you classes
            View v = inflater.inflate(R.layout.tab2, container, false);




            final FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();



            mDatabase = FirebaseDatabase.getInstance().getReference();
         //userID being receiverUID in database
            Query query = mDatabase.child("reminders").orderByChild("receiverUID_status").equalTo(MainActivity.userID+"_"+"active");

            //Setting size of recycler view as constant
            recyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view2);
            recyclerView.setHasFixedSize(true);

            //Setting Linear Layout
            layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());


            adapter = new FirebaseRecyclerAdapter<Reminder,ReminderHolder>(
                    Reminder.class,
                    R.layout.cards_layout,
                    ReminderHolder.class,
                    query){


                @Override
                protected void populateViewHolder(ReminderHolder holder, Reminder reminder, final int position) {
                    //Setting the name,message and time
                    holder.setName(reminder.getSenderName());
                    holder.setMessage(reminder.getReminderMessage());
                    holder.button_reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            adapter.getRef(position).child("receiverUID_status").setValue("reject");
                            adapter.notifyItemRemoved(position);

                        }
                    });


                }
            };

    return v
}

ReminderHolder class

public class ReminderHolder extends RecyclerView.ViewHolder {



    private final TextView mNameField;
    private final TextView mMessageField;
    public final Button button_reject;

    public ReminderHolder(View itemView) {
        super(itemView);
        mNameField = (TextView) itemView.findViewById(R.id.name);
        mMessageField = (TextView) itemView.findViewById(R.id.text);
        button_reject=(Button)itemView.findViewById(R.id.button_reject);

    }

    private void rejectreminder(int adapterPosition) {



    }


    public void setName(String name) {
        mNameField.setText(name);
    }

    public void setMessage(String message) {
        mMessageField.setText(message);
    }


}

Reminder class

public class Reminder {


    public String reminderMessage;
    public String senderUID;
    public String senderName;
    public  String receiverUID;
    public  String receiverName;
    public  String reminderTime;
    public  String timestamp;
    public String status;
    public  String receiverUID_status;

    public Reminder() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public Reminder(String reminderMessage,String senderUID,String senderName,String receiverUID,String receiverName,String reminderTime,String timestamp,String status,String receiverUID_status) {
        this.reminderMessage = reminderMessage;
        this.senderUID =senderUID ;
        this.senderName=senderName;


        this.receiverUID=receiverUID;
        this.receiverName=receiverName;

        this.reminderTime=reminderTime;
        this.timestamp=timestamp;
        this.status=status;
        this.receiverUID_status=receiverUID_status;
    }

Solution

  • You'll first have to change the way you store the date to either an actual unix timestamp, or the date-time in textual format that can be sorted easily. See:

    So the better formats are:

    • 1500241488157 (timestamp right now)
    • 2017-07-16T23:45:05 (sortable time right now)

    You can then query for upcoming items with:

    Query query = ref.orderByChild("timestamp").startAt(System.currentTimeMillis());
    query.addChildEventListener(new ChildEventListener() {
      void onChildAdded(DataSnapshot snapshot, String previousKey) {
        System.out.println(snapshot.getKey()+": "+snapshot.getChild("receiverName").getValue(String.class));