[英]Firebase getting ID for specific node to delete/update
first time using Firebase and pretty new to android studio, i'm trying to make a schedule app where a user can create many schedules and it would be associated to their account.第一次使用 Firebase 并且是 android 工作室的新手,我正在尝试制作一个时间表应用程序,用户可以在其中创建许多时间表并将其关联到他们的帐户。
At the moment a user can create an account, create a schedule (i just have 2 fields for this, will add the rest once i get the issue sorted) and also have multiple schedules.目前,用户可以创建一个帐户,创建一个时间表(我只有 2 个字段,一旦我解决了问题,就会添加 rest)并且还有多个时间表。
I would like to be able to update/delete a schedule of a user but i'm struggling to get the ID of the specific schedule node in which I need to delete.我希望能够更新/删除用户的时间表,但我正在努力获取我需要删除的特定时间表节点的 ID。
This is what i have in Firebase with a single user and 2 schedules这是我在 Firebase 中拥有的一个用户和 2 个时间表
EDITED FROM HERE从这里编辑
Part of ScheduleActivity.java ScheduleActivity.java 的一部分
What the problem is问题是什么
I have a listView with all the schedules that has a listener我有一个 listView,其中包含所有具有侦听器的时间表
scheduleId = scheduleList.get(position).getScheduleId();
public class ScheduleActivity extends AppCompatActivity implements View.OnClickListener {
private List<Schedule> scheduleList;
private List<String> scheduleIdList;
private DatabaseReference scheduleReference;
private String userId;
public static final String SCHEDULE_TITLE = "title";
public static final String SCHEDULE_DESCRIPTION = "description";
private String scheduleId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
getSupportActionBar().hide();
scheduleList = new ArrayList<>();
scheduleIdList = new ArrayList<>();
userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
scheduleReference = FirebaseDatabase.getInstance().getReference(DBStrings.DB_SCHEDULES);
registerForContextMenu(scheduleListView);
// when a user clicks any of list items
scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Schedule schedule = scheduleList.get(position);
// this gets the id but the issue is i need to first click the list item then ill get the correct id, otherwise i get a NPE because i haven't accessed the list item yet
// need to figure out how to implement this in the onStart() method so i can get the scheduleId beforehand
scheduleId = scheduleList.get(position).getScheduleId();
// intent that takes me to the activity to view the schdule details
Intent viewSchedule = new Intent(ScheduleActivity.this, ViewSchedule.class);
viewSchedule.putExtra(SCHEDULE_TITLE, schedule.getTitle());
viewSchedule.putExtra(SCHEDULE_DESCRIPTION, schedule.getDescription());
startActivity(viewSchedule);
}
}
});
}
// when a user long clicks a list item, brings up menu with option to edit/delete
// Also display a toast with scheduleID so i can see if the proper id is being retrieved
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Toast.makeText(ScheduleActivity.this, "id: " + scheduleId, Toast.LENGTH_SHORT).show();
getMenuInflater().inflate(R.menu.schedule_menu, menu);
}
// switch statement for delete/ redirect to edit activity that i left out
// load schedule data into list view
@Override
protected void onStart() {
super.onStart();
scheduleReference.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
scheduleList.clear();
scheduleIdList.clear();
for(DataSnapshot dataSnapshot : snapshot.getChildren()) {
Schedule schedule = dataSnapshot.getValue(Schedule.class);
// add schedules to list to show in list view
scheduleList.add(schedule);
// Add all the ids of schedules in a list, i used this in my scheduleListView.setOnItemListener to grab the scheduleId.
scheduleIdList.add(schedule.getScheduleId());
}
System.out.println("id list: " + scheduleIdList);
ScheduleListAdapter adapter = new ScheduleListAdapter(ScheduleActivity.this, scheduleList);
scheduleListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
System.out.println("ERROR: " + error.toException());
}
});
}
private void deleteSchedule(String scheduleId) {
// At the moment i can only delete a item if i first view it by clicking, then i need to go back and it allows me to delete it, this is obviously because my listview listener issue (It does not let me delete without first clicking the item to view/access it) scheduleReference.child(userId).child(scheduleId).removeValue();
Toast.makeText(ScheduleActivity.this, "Schedule " + scheduleId + " was deleted!", Toast.LENGTH_LONG).show();
}
}
List view of schedules时间表的列表视图
The issue is in the scheduleListView.setOnItemClickListener
, i need to find a way to grab the id maybe in onStart method or somewhere eother than the listener, but since I do not have access to the position like i did here, i am struggling to implement this.问题出在
scheduleListView.setOnItemClickListener
中,我需要找到一种方法来获取 id,可能是在 onStart 方法中,也可能是在监听器以外的其他地方,但由于我无法像在这里那样访问 position,所以我正在努力实施这个。
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Schedule schedule = scheduleList.get(position);
// Talking about this <position>
scheduleId = scheduleList.get(position).getScheduleId();
}
Images to explain it better图片可以更好地解释它
I hope it makes sense, i would need to access each list item then go back to be able to delete a specific one.我希望这是有道理的,我需要访问每个列表项,然后返回 go 才能删除特定的列表项。
private void deleteSchedule(String scheduleId) {
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
scheduleReference.child(userId).child(scheduleId).removeValue();
Toast.makeText(ScheduleActivity.this, "Schedule " + scheduleId + " was deleted!", Toast.LENGTH_LONG).show();
}
You have to get your scheduleId when the user clicks on delete schedule Only Then you can get the original path of the schedule and can remove that schedule当用户单击删除计划时,您必须获取您的 scheduleId 然后您可以获得计划的原始路径并可以删除该计划
If you don't know how to get a specific schedule id then I can help you with that too.如果您不知道如何获取特定的日程表 ID,那么我也可以帮助您。 but you need recyclerview for that.
但你需要 recyclerview。 you can simply achieve that with recyclerview and interface
你可以简单地通过 recyclerview 和界面来实现
Updated Answer for updated Question Maybe this can help you.更新问题的更新答案也许这可以帮助你。 I have added LongClick Listner Here you can do the same as well.
我在此处添加了 LongClick Listner,您也可以这样做。 I don't have exp with listview but my logic says it will work in your case.
我没有带有 listview 的 exp,但我的逻辑说它适用于你的情况。 also, try to learn recyclerview.
另外,尝试学习recyclerview。 because recyclerview is needed for every android developer.
因为每个 android 开发者都需要 recyclerview。
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
PopupMenu popup = new PopupMenu(Test.this, view);
popup.getMenuInflater().inflate(R.menu.schedule_menu, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
// You can compare here your menu id and perform action as id like item.getItemId() == R.id.delete
// also get Item by position like scheduleListView.getAdapter().getItem(position);
Toast.makeText(Test.this,"You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
});
popup.show();//showing popup menu
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.