gpt4 book ai didi

Android:在 EditText 中检索和显示 Firebase 数据,对其进行编辑并保存内容

转载 作者:行者123 更新时间:2023-11-29 19:08:12 25 4
gpt4 key购买 nike

如何从 Firebase 编辑特定日期?我想要的是,在 ListView 中,他们是一个事件列表,然后用户想要通过在特定事件中执行长按来编辑它。如果用户已经执行了长按,他们将有两个选项,删除或编辑事件。我已经做了删除方法,但我的问题是更新方法。其中一旦用户点击选项中的更新,它将转到另一个 Activity ,用户选择编辑的事件的所有数据将显示在一个编辑文本上,用户已经可以编辑该事件。我将如何做?

我希望你能帮助我。谢谢!

这是我的代码:

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) {

}
});
}

最佳答案

在事件对象中,您仍然可以输入要更新的日期的子路径并设置值。

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");

关于Android:在 EditText 中检索和显示 Firebase 数据,对其进行编辑并保存内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com