Search in sources :

Example 36 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project GogoNew by kuldeep725.

the class MapsActivity method getNearestBus.

private void getNearestBus() {
    minimumDistance = Double.MAX_VALUE;
    if (mCurrentLocation == null)
        return;
    if (mDatabase != null) {
        final DatabaseReference userDatabase = mDatabase.child(USER);
        userDatabase.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                keyStr = dataSnapshot.getKey();
                Log.d(TAG, "keyStr = " + keyStr);
                Log.d(TAG, "myDataSnapShot = " + dataSnapshot);
                Log.d(TAG, "dataSnapshot.child(keyStr).child(\"location\").child(\"latitude\") : " + dataSnapshot.child("location").child("latitude"));
                String latStr = (String) dataSnapshot.child("location").child("latitude").getValue();
                String lngStr = (String) dataSnapshot.child("location").child("longitude").getValue();
                Log.d(TAG, "latStr = " + latStr);
                Log.d(TAG, "lngStr = " + lngStr);
                Double latdub = Double.parseDouble(latStr);
                Double lngdub = Double.parseDouble(lngStr);
                // Double diff = SphericalUtil.computeDistanceBetween(
                // new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()),
                // new LatLng(latdub, lngdub));
                diff = CalculationByDistance(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), new LatLng(latdub, lngdub)) * 1000;
                Log.d(TAG, "diff = " + diff);
                if (diff < minimumDistance) {
                    minimumDistance = diff;
                    // minimumLocation = new LatLng(latdub, lngdub);
                    if (keyStr.contains("b"))
                        radiobuttonId = Integer.parseInt(keyStr.split("b")[1]);
                }
                Log.d(TAG, "radiobuttonId = " + radiobuttonId);
            // if (mCurrentLocation != null) {
            // String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
            // + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude()
            // + "&destination=" + latStr + "," + lngStr + "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";// + R.string.google_direction_api_key;
            // /*String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
            // + "40.81649,-73.907807&destination=40.819585,-73.90177"+ "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";// + R.string.google_direction_api_key;*/
            // //                                         Log.d(TAG, "URL : " + url);
            // DownloadTask downloadTask = new DownloadTask();
            // //                                        Log.d(TAG, "@busDistance (SphericalUtil.computeDistanceBetween) = " + SphericalUtil.computeDistanceBetween(mCurrentPosition, new LatLng(Double.parseDouble(latitudeStr), Double.parseDouble(longitudeStr))));
            // // Start downloading json data from Google Directions API
            // downloadTask.execute(url);
            // }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
        userDatabase.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d(TAG, "@addListener radioButtonId = " + radiobuttonId);
                radiobutton = (RadioButton) findViewById(radiobuttonId);
                checkBusSelection = radiobuttonId;
                if (lastButton != null) {
                    Log.d(TAG, "LASTBUTTON = " + lastButton.toString());
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        lastButton.setBackground(getResources().getDrawable(R.drawable.radio_button_event));
                        lastButton.setPaintFlags(radiobutton.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG));
                        if (lastButton != pickMeRadioButton) {
                            lastButton.setTypeface(Typeface.DEFAULT);
                        }
                    }
                    if (lastButton != pickMeRadioButton) {
                        lastButton.setTextColor(Color.BLACK);
                    }
                }
                radiobutton.setTextColor(Color.parseColor(primeColorString));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    radiobutton.setBackground(getDrawable(R.drawable.underline));
                } else {
                    radiobutton.setPaintFlags(radiobutton.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    radiobutton.setBackgroundColor(Color.parseColor("#FFFFFF"));
                }
                if (mMap != null) {
                    mMap.clear();
                    onMapReady(mMap);
                }
                makeMarkerOnTheLocation();
                showMarkers();
                // previousSelection = -1;
                // for using google distance api in showDistanceInBetween
                flagShowDistanceInBetween = 0;
                showDistanceInBetween();
                // String dist = distance.getText().toString();
                Log.e(TAG, "minDistance = " + minimumDistance);
                long distLong = Math.round(minimumDistance);
                String distStr;
                if (minimumDistance < 1000) {
                    distStr = String.valueOf(distLong) + " m";
                    Log.d(TAG, "distStr = " + distStr);
                // long timeLong =Math.round(9 * distLong / 100);          //      40km/hr into m/s
                } else {
                    distLong = Math.round(distLong / 1000);
                    distStr = String.valueOf(distLong + " km");
                    Log.d(TAG, "distStr = " + distStr);
                }
                String str = "The nearest bus is Bus " + checkBusSelection + " at distance " + distStr;
                Toast.makeText(MapsActivity.this, str, Toast.LENGTH_SHORT).show();
                // Log.d(TAG, "minDistStr = " + minDistStr);
                // Log.d(TAG, "minimum Time = " + minimumTime);
                // distance.setText(minDistStr);
                // duration.setText(minimumTime);
                lastButton = radiobutton;
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) LatLng(com.google.android.gms.maps.model.LatLng) ValueEventListener(com.google.firebase.database.ValueEventListener) RadioButton(android.widget.RadioButton) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 37 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project SEProject by NicholasBarreyre.

the class TeamDetailActivity method populateMemberList.

/**
 * Populates the list of teams associated with the user
 */
private void populateMemberList() {
    final ListView teamMemberListView = findViewById(R.id.memberList);
    // retrieve database reference to the team members
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams/" + team.getId() + "/teamMembers");
    teamMemberAdapter = new TeamMembersAdapter(this, new ArrayList<String>());
    teamsReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            teamMemberAdapter.add(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            teamMemberAdapter.remove(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    teamMemberListView.setAdapter(teamMemberAdapter);
    teamMemberListView.setOnItemClickListener(new TeamDetailActivity.TeamMemberListClickListener());
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) TeamMembersAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamMembersAdapter) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 38 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project SEProject by NicholasBarreyre.

the class SearchResultsActivity method populateListWithSearchResults.

private void populateListWithSearchResults(final String query) {
    // Get the reference to the UI contents
    final ListView teamListView = findViewById(R.id.teamList);
    // retrieve database reference to the teams
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams");
    teamAdapter = new TeamsAdapter(this, new ArrayList<Team>());
    teamsReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            // TODO: Don't add teams already a member of
            if (dataSnapshot.getValue(Team.class).getName().toUpperCase().contains(query.toUpperCase())) {
                teamAdapter.add(dataSnapshot.getValue(Team.class));
                teamAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            // TODO: Don't add teams already a member of
            if (dataSnapshot.getValue(Team.class).getName().toUpperCase().contains(query.toUpperCase())) {
                teamAdapter.add(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1) {
                teamAdapter.remove(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            Log.d("TeamList", dataSnapshot.getValue(Team.class).getName() + "has moved locations...");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    teamListView.setAdapter(teamAdapter);
    teamListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Team team = teamAdapter.getItem(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(SearchResultsActivity.this);
            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("Apply", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final AlertDialog.Builder confirmDialog = new AlertDialog.Builder(SearchResultsActivity.this);
                    confirmDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    confirmDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            TeamManager.requestToJoinTeam(team, user);
                            dialog.dismiss();
                            finish();
                        }
                    });
                    confirmDialog.setTitle("Confirm Application").setMessage("Are you sure you want to apply to " + team.getName() + "?").show();
                }
            });
            builder.setTitle("Apply To Team").setMessage("\nDo you want to apply to " + team.getName()).show();
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) TeamsAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

ChildEventListener (com.google.firebase.database.ChildEventListener)38 DataSnapshot (com.google.firebase.database.DataSnapshot)38 DatabaseError (com.google.firebase.database.DatabaseError)38 DatabaseReference (com.google.firebase.database.DatabaseReference)11 Intent (android.content.Intent)7 View (android.view.View)5 Product (com.example.asus.onlinecanteen.model.Product)5 Map (java.util.Map)5 ListView (android.widget.ListView)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 BrixxEventModel (com.manan.dev.ec2018app.Models.BrixxEventModel)4 TextView (android.widget.TextView)3 Transaction (com.example.asus.onlinecanteen.model.Transaction)3 LatLng (com.google.android.gms.maps.model.LatLng)3 Query (com.google.firebase.database.Query)3 HashMap (java.util.HashMap)3 AlertDialog (android.support.v7.app.AlertDialog)2 Button (android.widget.Button)2 ImageView (android.widget.ImageView)2 RadioButton (android.widget.RadioButton)2