use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class FirebaseUtils method removeExpenseFirebase.
public void removeExpenseFirebase(final String expenseID, final Context context) {
databaseReference.child("expenses").child(expenseID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String groupID = dataSnapshot.child("groupID").getValue(String.class);
// aggiunto da riky per mandare la notifica a tutti tranne a chi ha eliminato la spesa
databaseReference.child("expenses").child(expenseID).child("deletedBy").setValue(MainActivity.getCurrentUID());
// Elimino spesa dal gruppo
databaseReference.child("groups").child(groupID).child("expenses").child(expenseID).setValue(false);
// Per ogni participant elimino la spesa dal suo elenco spese
for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
String participantID = participantSnapshot.getKey();
databaseReference.child("users").child(participantID).child("expenses").child(expenseID).setValue(false);
}
// Elimino commenti sulla spesa
databaseReference.child("comments").child(groupID).removeValue();
// Elimino spesa
databaseReference.child("expenses").child(expenseID).child("deleted").setValue(true);
// Toast.makeText(context,context.getString(R.string.expense_removed),Toast.LENGTH_SHORT).show();
// add event for EXPENSE_REMOVE
databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User currentUser = MainActivity.getCurrentUser();
Event event = new Event(dataSnapshot.child("groupID").getValue(String.class), Event.EventType.EXPENSE_REMOVE, currentUser.getName() + " " + currentUser.getSurname(), dataSnapshot.child("description").getValue(String.class));
event.setDate(new SimpleDateFormat("yyyy.MM.dd").format(new java.util.Date()));
event.setTime(new SimpleDateFormat("HH:mm").format(new java.util.Date()));
FirebaseUtils.getInstance().addEvent(event);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, databaseError.toException());
}
});
/*Intent intent = new Intent(context, GroupDetailActivity.class);
intent.putExtra("groupID", groupID);
intent.putExtra("userID", MainActivity.getCurrentUID());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.
the class NewPostActivity method submitPost.
private void submitPost() {
final String title = mTitleField.getText().toString();
final String body = mBodyField.getText().toString();
// Title is required
if (TextUtils.isEmpty(title)) {
mTitleField.setError(REQUIRED);
return;
}
// Body is required
if (TextUtils.isEmpty(body)) {
mBodyField.setError(REQUIRED);
return;
}
// Disable button so there are no multi-posts
setEditingEnabled(false);
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
// [START single_value_read]
final String userId = getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
// [START_EXCLUDE]
if (user == null) {
// User is null, error out
Log.e(TAG, "User " + userId + " is unexpectedly null");
Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
} else {
// Write new post
writeNewPost(userId, user.username, title, body);
}
// Finish this Activity, back to the stream
setEditingEnabled(true);
finish();
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
// [START_EXCLUDE]
setEditingEnabled(true);
// [END_EXCLUDE]
}
});
// [END single_value_read]
}
use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.
the class PostDetailActivity method onStart.
@Override
public void onStart() {
super.onStart();
// Add value event listener to the post
// [START post_value_event_listener]
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// [START_EXCLUDE]
mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mPostListener = postListener;
// Listen for comments
mAdapter = new CommentAdapter(this, mCommentsReference);
mCommentsRecycler.setAdapter(mAdapter);
}
use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.
the class NewPostActivity method submitPost.
private void submitPost() {
final String title = mTitleField.getText().toString();
final String body = mBodyField.getText().toString();
// Title is required
if (TextUtils.isEmpty(title)) {
mTitleField.setError(REQUIRED);
return;
}
// Body is required
if (TextUtils.isEmpty(body)) {
mBodyField.setError(REQUIRED);
return;
}
// Disable button so there are no multi-posts
setEditingEnabled(false);
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
// [START single_value_read]
final String userId = mAuth.getCurrentUser().getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
// [START_EXCLUDE]
if (user == null) {
// User is null, error out
Log.e(TAG, "User " + userId + " is unexpectedly null");
Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
} else {
// Write new post
writeNewPost(userId, user.username, title, body);
}
// Finish this Activity, back to the stream
setEditingEnabled(true);
finish();
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
// [START_EXCLUDE]
setEditingEnabled(true);
// [END_EXCLUDE]
}
});
// [END single_value_read]
}
use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.
the class PostDetailActivity method onStart.
@Override
public void onStart() {
super.onStart();
// Add value event listener to the post
// [START post_value_event_listener]
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// [START_EXCLUDE]
mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mPostListener = postListener;
// Listen for comments
mAdapter = new CommentAdapter(this, mCommentsReference);
mCommentsRecycler.setAdapter(mAdapter);
}
Aggregations