Search in sources :

Example 31 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project EC2018App by Manan-YMCA.

the class StarNightFragment method attachDatabaseListener.

private void attachDatabaseListener() {
    if (mChildEventListener == null) {
        mChildEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.getKey().equals("starnight")) {
                    BrixxEventModel w = dataSnapshot.getValue(BrixxEventModel.class);
                    updateUI(w);
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                try {
                    if (dataSnapshot.getKey().equals("starnight")) {
                        BrixxEventModel w = dataSnapshot.getValue(BrixxEventModel.class);
                        updateUI(w);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

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

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };
        mDatabaseReference.addChildEventListener(mChildEventListener);
    }
}
Also used : BrixxEventModel(com.manan.dev.ec2018app.Models.BrixxEventModel) DatabaseError(com.google.firebase.database.DatabaseError) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 32 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project FindMyHome by DjangoBlockchained.

the class ShelterDetailActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    Intent i = getIntent();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shelter_details);
    ListView list = findViewById(R.id.shelterdetails);
    Button claimButton = findViewById(R.id.claimbutton);
    adapter = new ArrayAdapter<>(ShelterDetailActivity.this, android.R.layout.simple_list_item_1, details);
    username = i.getStringExtra("username");
    String name = i.getStringExtra("Shelter Name");
    FirebaseDatabase shelterDB = FirebaseDatabase.getInstance();
    DatabaseReference shelterRefPre = shelterDB.getReference();
    DatabaseReference shelterRef = shelterRefPre.child("shelters");
    DatabaseReference nameRef = shelterRef.child(name);
    claimButton.setOnClickListener((view) -> {
        Intent intent = new Intent(ShelterDetailActivity.this, ClaimScreenActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("Shelter Name", name);
        startActivity(intent);
    });
    nameRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Object data = dataSnapshot.getValue();
            details.add(getLabel(dataSnapshot.getKey(), data));
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Object data = dataSnapshot.getValue();
            String key = dataSnapshot.getKey();
            // Find the current row for the key and replace it with the new value.
            int index = -1;
            boolean found = false;
            for (int i = 0; (i < details.size()) && (!found); i++) {
                String ss = details.get(i);
                if (ss.contains(key)) {
                    index = i;
                    found = true;
                }
            }
            if (index != -1) {
                details.set(index, getLabel(dataSnapshot.getKey(), data));
            } else {
                details.add(getLabel(dataSnapshot.getKey(), data));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

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

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    list.setAdapter(adapter);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) Button(android.widget.Button) DatabaseReference(com.google.firebase.database.DatabaseReference) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 33 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project FindMyHome by DjangoBlockchained.

the class ShelterListActivity method showSearchedShelter.

private void showSearchedShelter(String name, CharSequence gender, CharSequence age) {
    shelterRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Object value = dataSnapshot.getValue();
            Map string = (Map) value;
            if (string != null) {
                Object o = string.get("restrictions");
                if (o != null) {
                    Object nameObj = string.get("name");
                    if (matchesSearch(string, name, gender, age)) {
                        names.add(nameObj.toString());
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }

        @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) {
        }
    });
    list.setAdapter(adapter);
    list.setOnItemClickListener((parent, view, position, id) -> {
        String selected = (String) list.getItemAtPosition(position);
        Intent intent = new Intent(ShelterListActivity.this, ShelterDetailActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("Shelter Name", selected);
        startActivity(intent);
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 34 with ChildEventListener

use of com.google.firebase.database.ChildEventListener in project iosched by google.

the class FeedPresenter method initializeDataListener.

@Override
public void initializeDataListener(DatabaseReference databaseReference) {
    if (mEventListener == null) {
        mEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.addFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.updateFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.removeFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                try {
                    FeedMessage feedMessage = dataSnapshot.getValue(FeedMessage.class);
                    mView.updateFeedMessage(feedMessage);
                } catch (DatabaseException e) {
                    mView.showErrorMessage();
                    LOGE(TAG, "Firebase error - " + e);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                mView.showErrorMessage();
            }
        };
    }
    databaseReference.addChildEventListener(mEventListener);
}
Also used : FeedMessage(com.google.samples.apps.iosched.feed.data.FeedMessage) DatabaseError(com.google.firebase.database.DatabaseError) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseException(com.google.firebase.database.DatabaseException) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 35 with ChildEventListener

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

the class MapsActivity method showMarkers.

private void showMarkers() {
    if (checkBusSelection != 0) {
        showInternetStatus();
        if (!isInternetOn())
            return;
        // Log.d(TAG, "showMarkers IS FIRED...");
        String BUS = "b" + checkBusSelection;
        final DatabaseReference routeDatabase = mDatabase.child(USER).child(BUS).child(ROUTE);
        routeDatabase.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String bus = "b" + checkBusSelection;
                if (!routeDatabase.toString().equals(mDatabase.child(USER).child(bus).child(ROUTE).toString()))
                    return;
                GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
                };
                // Log.d(TAG, "Data : " + dataSnapshot.getValue(genericTypeIndicator));
                Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
                assert map != null;
                String latitudeStr = map.get("latitude");
                String longitudeStr = map.get("longitude");
                // Log.d(TAG, "Latitude = " + latitudeStr);
                // Log.d(TAG, "Longitude = " + longitudeStr);
                double latitude = Double.parseDouble(latitudeStr);
                double longitude = Double.parseDouble(longitudeStr);
                String busName = "BUS " + checkBusSelection;
                // Geocoder geocoder = new Geocoder(getApplicationContext());
                // try {
                // List<android.location.Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                // String str = "";
                // if (addressList.get(0).getSubLocality() != null) {
                // str = addressList.get(0).getSubLocality()+",";
                // }
                // str += addressList.get(0).getLocality();
                // str += addressList.get(0).getCountryName();
                String str = "";
                str += /*" (" + */
                dataSnapshot.getKey();
                LatLng latlng = new LatLng(latitude, longitude);
                Marker marker = mMap.addMarker(new MarkerOptions().position(latlng).title(str));
                marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.red_stop_10));
                markerMap.put(latlng, marker);
                markerNameList.put(latlng, str);
            // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
            // }
            // catch (IOException e) {
            // e.printStackTrace();
            // mMap.addMarker(new MarkerOptions()
            // .position(new LatLng(latitude, longitude))
            // .title(busName))
            // .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.red_stop_10));
            // //                          mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
            // Log.e(TAG, "GEOCODER DIDN'T WORK.");
            // }
            }

            @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) {
            }
        });
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) LatLng(com.google.android.gms.maps.model.LatLng) Marker(com.google.android.gms.maps.model.Marker) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap) 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