Reputation: 81
How to edit a specific date from firebase? What i want is that in a listview their is a list of events, then the user wants to edit it by performing a long click in a specific event. If the user already performed the long click their will be two options, to delete or edit the event. I already did the delete method but my problem is the update method. In which once the user clicked the update in the option, it will go to another activity and all the data of the event that the user choose to edit will be displayed on an edittext and the user can already edit the event. How will I do that?
I hope you could help me. Thank you!
Here is my code:
databaseReference = FirebaseDatabase.getInstance().getReference("events_users").child(userUid);
listViewEvents = (ListView) findViewById(R.id.listViewEvents);
eventInfos = new ArrayList<>();
// listViewEvents.setLongClickable(true);
listViewEvents.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
EventInfo eventInfo = eventInfos.get(i);
showUpdateDeleteDialog(eventInfo.getEventId(), eventInfo.getEventTitle());
return true;
}
});
private void showUpdateDeleteDialog(final String eventId, String eventTitle){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.update_delete_dialog, null);
dialogBuilder.setView(dialogView);
final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateEvent);
final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteEvent);
dialogBuilder.setTitle(eventTitle);
final AlertDialog b = dialogBuilder.create();
b.show();
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteEvent(eventId);
b.dismiss();
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventInfo eventInfo = new EventInfo();
Intent intent = new Intent(_8_ViewEventMember.this, EventUpdate.class);
startActivity(intent);
}
});
}
private boolean deleteEvent (String id) {
//getting the specified artist reference
auth = FirebaseAuth.getInstance();
String userUid1 = auth.getCurrentUser().getUid();
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("events_users").child(userUid1).child(id);
DatabaseReference dR1 = FirebaseDatabase.getInstance().getReference("events").child(id);
//removing artist
dR.removeValue();
dR1.removeValue();
Toast.makeText(getApplicationContext(), "Event Deleted", Toast.LENGTH_LONG).show();
return true;
}
private boolean updateEvent(String id, String title, String dateStart, String dateEnd, String timeStart, String timeEnd,
String location,
String description){
auth = FirebaseAuth.getInstance();
String userUid1 = auth.getCurrentUser().getUid();
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("events_users").child(userUid1).child(id);
DatabaseReference dR1 = FirebaseDatabase.getInstance().getReference("events").child(id);
EventInfo eventInfo = new EventInfo(id, title, dateStart, dateEnd, timeStart, timeEnd, location, description);
dR.setValue(eventInfo);
dR1.setValue(eventInfo);
Toast.makeText(getApplicationContext(), "Event Updated", Toast.LENGTH_LONG).show();
return true;
}
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
eventInfos.clear();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()){
EventInfo eventInfo = postSnapshot.getValue(EventInfo.class);
eventInfos.add(eventInfo);
}
EventInfo_Adapter eventInfoAdapter = new EventInfo_Adapter(_8_ViewEventMember.this, eventInfos);
listViewEvents.setAdapter(eventInfoAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Upvotes: 1
Views: 706
Reputation: 2449
Inside the event object you can still enter the child path for the date you want to update and set the value.
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("events_users").child(userUid1).child(id);
DatabaseReference dR1 = FirebaseDatabase.getInstance().getReference("events").child(id);
DatabaseReference dateReference = dR.child("date_path");
DatabaseReference dateReference1 = dR1.child("date_path");
dateReference.setValue("new_date");
dateReference1.setValue("new_date");
Upvotes: 2