private void connectToFireBase() { rootRef.addChildEventListener( new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { ChatRoom chatRoom = dataSnapshot.getValue(ChatRoom.class); Log.d(TAG, "Chat message Added. Text = " + chatRoom.getId()); updateAdapter(chatRoom); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { Log.d(TAG, "onChildChanged " + s); } @Override public void onChildRemoved(DataSnapshot dataSnapshot) {} @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) {} @Override public void onCancelled(FirebaseError firebaseError) {} }); }
private void setupIntentAnswerUpdates() { newMessageListener = messagesRef.addChildEventListener( new ChildEventListener() { @Override public void onChildRemoved(DataSnapshot arg0) { // TODO Auto-generated method stub } @Override public void onChildMoved(DataSnapshot arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onChildChanged(DataSnapshot arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onChildAdded(DataSnapshot dataSnapshot, String arg1) { Message model = dataSnapshot.getValue(Message.class); if (!username.equals(model.getAuthor())) { intentAnswer.put("read", true); } } @Override public void onCancelled(FirebaseError arg0) { // TODO Auto-generated method stub } }); }
// Method to add the neccessary event listeners to catch any new message entries on the database protected void startReceivingMessages() { // Create Event listener for all events(reading etc) mainListener = new ChildEventListener() { // Retrieve new posts as they are added to the database - NB!!! @Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { Map<String, String> post = snapshot.getValue(Map.class); // Get the Map object that was written System.out.println("Message is :" + post.get("Message")); mainView.receiveMessage(post.get("User"), post.get("Message")); } // METHODS NEEDED TO BE IMPLEMENTED FOR EVENT LISTENER - MUST BE ADDED TO // Get the data on a post that has changed @Override public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {} // Get the data on a post that has been removed @Override public void onChildRemoved(DataSnapshot snapshot) {} @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) {} @Override public void onCancelled(FirebaseError firebaseError) {} }; // Add event listener on database myFirebaseRef.addChildEventListener(mainListener); }
@Override protected void onCreate(Bundle savedInstanceState) { activ = this; dishes = new ArrayList<String>(20); super.onCreate(savedInstanceState); setContentView(R.layout.activity_orders); // Initialize the UI components foodList = (ListView) findViewById(R.id.listView1); // Create a reference to a Firebase location ref = new Firebase( "https://uqtez5y2bki.firebaseio-demo.com/Restaurants/Restaurant 2/Tables/Table 2/Orders"); // Read data and react to changes ref.addChildEventListener( new ChildEventListener() { @Override public void onChildAdded(DataSnapshot snapshot, String previousChildName) { Map s = snapshot.getValue(Map.class); dishes.add( snapshot.getName() + " " + ((Map) snapshot.getValue(Map.class)).get("Price")); Log.i("Test", dishes.toString()); arrayAdapter = new ArrayAdapter(activ, android.R.layout.simple_list_item_1, dishes.toArray()); // By using setAdapter method, you plugged the ListView with adapter foodList.setAdapter(arrayAdapter); } @Override public void onChildChanged(DataSnapshot snapshot, String previousChildName) {} @Override public void onChildRemoved(DataSnapshot snapshot) { Map s = snapshot.getValue(Map.class); dishes.remove( snapshot.getName() + " " + ((Map) snapshot.getValue(Map.class)).get("Price")); Log.i("Test", dishes.toString()); arrayAdapter = new ArrayAdapter(activ, android.R.layout.simple_list_item_1, dishes.toArray()); // By using setAdapter method, you plugged the ListView with adapter foodList.setAdapter(arrayAdapter); } @Override public void onChildMoved(DataSnapshot snapshot, String previousChildName) {} @Override public void onCancelled(FirebaseError arg0) {} }); foodList.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { view.setBackgroundColor(Color.GREEN); String text = dishes.get(position); String str = text.substring(text.indexOf(" ")); int num = Integer.parseInt(str.trim()); total += num; } }); }