Firebase 获取特定节点的 ID delete/update

Firebase getting ID for specific node to delete/update

第一次使用 Firebase 并且对 android studio 还很陌生,我正在尝试制作一个日程应用程序,用户可以在其中创建 多个 日程表,它将是关联到他们的帐户。

目前用户可以创建一个帐户,创建一个时间表(我只有 2 个字段,一旦我解决了问题就会添加其余部分)并且还有多个时间表。

我希望能够 update/delete 一个用户的时间表,但我正在努力获取我需要删除的特定时间表节点的 ID。


这是我在 Firebase 中拥有的一个用户和 2 个时间表

从这里编辑


部分ScheduleActivity.java

问题是什么


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


    }

}


日程列表视图


问题出在 scheduleListView.setOnItemClickListener 中,我需要找到一种方法来获取 ID,可能是在 onStart 方法中,也可能是在监听器以外的其他地方,但由于我无法像以前那样访问该位置在这里,我正在努力实现这一点。

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();
}


图片更好解释

我希望它有意义,我需要访问每个列表项然后返回才能删除特定的一个。

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();
}

当用户点击删除日程时,你必须得到你的scheduleId 只有这样你才能得到时间表的原始路径并且可以删除那个时间表

如果您不知道如何获取特定的日程表 ID,那么我也可以帮助您。但你需要 recyclerview。你可以简单地用 recyclerview 和 interface

来实现

更新问题的更新答案 也许这可以帮助你。 我添加了LongClick Listner 在这里你也可以这样做 我没有带有 listview 的 exp,但我的逻辑说它适用于你的情况。 另外,尝试学习recyclerview。因为每个 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;
    }
});