jaswanth03
3/14/2018 - 11:28 AM

For selecting particular data from the firebase node.

using this we can select a particular data/value from a node by giving the path.

 // TODO: 03-01-2018  
        // to display the status of the user on the topToolBar of the Retichat Application.
        DatabaseReference databaseReference = database.getReference("user").child(ownerID).child("status");
         databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    String currentStatus = dataSnapshot.getValue(String.class);

                switch (currentStatus){
                    case "online":
                        TvChatToolbarStatus.setText(currentStatus);
                        break;
                    case "offline":
                        DatabaseReference databaseReference = database.getReference("user").child(ownerID).child("lastSeenTime");
                        databaseReference.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                long lastSeenTime = dataSnapshot.getValue(Long.class);

                                        //SimpleDateFormat timeFormat = new SimpleDateFormat("LLL dd,YYY h:mm a");
                                        SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
                                        String time = timeFormat.format(lastSeenTime);

                                //////////////// start of time and date format ////////////////////////////
                                long timeinmilllisec =lastSeenTime;
                                SimpleDateFormat timeDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm a");
                                SimpleDateFormat DBTime = new SimpleDateFormat("h:mm a");
                                SimpleDateFormat DBDate = new SimpleDateFormat("dd/MM/yyyy");

                                String databasetime = timeDateFormat.format(timeinmilllisec);
                                String finalDBTime = DBTime.format(timeinmilllisec);
                                String finalDBDate = DBDate.format(timeinmilllisec);

                                long presentdatetime = new Date().getTime();
                                String presentTime = timeDateFormat.format(presentdatetime);

                                String[] dbT = databasetime.split(" ");
                                String dbT1 = dbT[0];
                                String dbT2 = dbT[1];

                                String[] pT = presentTime.split(" ");
                                String pT1 = pT[0];
                                String pT2 = pT[1];

                                final Calendar cal = Calendar.getInstance();
                                cal.add(Calendar.DATE, -1);
                                Date yesterdayTime = cal.getTime();
                                String yesterdayDate = timeDateFormat.format(yesterdayTime);

                                String[] yT = yesterdayDate.split(" ");
                                String yT1 = yT[0];
                                String yT2 = yT[1];

                                if (pT1.equals(dbT1)){
                                    TvChatToolbarStatus.setText("last seen today at "+finalDBTime);
                                }
                                else if (yT1.equals(dbT1)){
                                    TvChatToolbarStatus.setText("last seen yesterday at"+finalDBTime);
                                }
                                else{
                                    TvChatToolbarStatus.setText("last seen "+finalDBDate);
                                }
                                //////////////// end of time and date format ////////////////////////////
                                }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                Log.e("MainActivity", "onCancelled", databaseError.toException());
                            }
                        });
                        break;
                }
                }catch (Exception e){
                    TvChatToolbarStatus.setText("offline");
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("MainActivity", "onCancelled", databaseError.toException());
            }
        });

    }