选择要更新的位置时如何正确使用Firestore ID

问题描述 投票:1回答:1

我正在努力使用Firebase Firestore获取更新。我目前可以点击我的RecyclerView中的一个位置,并从那里获取相关信息到我的编辑文本框中。但是,我目前的问题是我正在努力选择要更新的正确ID。我想弄清楚如何在更新时使用我的代码作为原始选择位置。

目前,当我点击位置时,我有一个显示正确ID和位置的Toast显示,然后它将带您进入我的更新活动,当我点击保存它时显示不正确的ID。我试图让这个ID匹配,以便我可以让我的更新工作正常。

我目前玩的是让代码正常工作,但没有运气。我做的一件事就是更改我的代码,这样当我点击更新时它会告诉我它选择了哪个ID。这样做会导致它选择错误的id /随机ID。这让我觉得我的问题在于选择ID。

ReadActivity

这是我在RecyclerView中选择位置时的代码。这当前正常工作,并选择正确的ID。


             public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                Book book = documentSnapshot.toObject(Book.class);
                String id = documentSnapshot.getId();
                String path = documentSnapshot.getReference().getPath();
                Toast.makeText(AdminReadActivity.this,
                        "Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();

                String chapterName = adapter.getItem(position).getChapterName();
                String chapterInfo = adapter.getItem(position).getChapterInfo();
                Integer chapterNumber = adapter.getItem(position).getChapterNumber();



                Intent intent = new Intent(AdminReadActivity.this, AdminUpdateActivity.class);

                intent.putExtra("mChapterName", chapterName);
                intent.putExtra("mChapterInfo", chapterInfo);
                intent.putExtra("mChapterNumber", chapterNumber);
                intent.putExtra("mMyId", id);

                startActivity(intent);

已编辑:UpdateActivity

这是我的updateBook方法。到目前为止,这只显示不正确的ID。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_icon:
                updateBook();
                Intent intent = new Intent(AdminUpdateActivity.this, AdminReadActivity.class);
                startActivity(intent);
            return true;
            default:
                return super.onOptionsItemSelected(item);
        }


private void updateBook() {

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        DocumentReference bookRef = db.collection("Book").document(id);
        bookRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                String id = documentSnapshot.getId();
                FirebaseFirestore db = FirebaseFirestore.getInstance();
                DocumentReference bookRef = db.collection("Book").document(id);
                Toast.makeText(AdminUpdateActivity.this, " ID: " + id, Toast.LENGTH_SHORT).show();
            }

        });
    }

BookAdapter

 itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && listener != null)//ensure the user does not click deleted items
                    {
                        listener.onItemClick(getSnapshots().getSnapshot(position), position);
                    }
                }
            });

 public interface OnItemClickListener    {
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }

当我在UpdateActivity中单击“保存”以显示与我从RecyclerView首次选择它时所执行的正确且相同的ID时,是否有人可以建议我如何获取ID。

在更改后,我现在从OnOptionSelected和UpdateBook()类中获取此错误。

编辑:添加错误日志

java.lang.NullPointerException: Provided document path must not be null.
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:906)
        at com.google.firebase.firestore.CollectionReference.document(com.google.firebase:firebase-firestore@@18.2.0:110)
        at com.example.home.optometryapplication.AdminUpdateActivity.updateBook(AdminUpdateActivity.java:155)
        at com.example.home.optometryapplication.AdminUpdateActivity.onOptionsItemSelected(AdminUpdateActivity.java:101)
        at android.app.Activity.onMenuItemSelected(Activity.java:3204)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
        at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:674)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
java android google-cloud-firestore
1个回答
1
投票

您需要通过OnItemClick方法将ID传递给UpdateActivity

public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
            Book book = documentSnapshot.toObject(Book.class);
            String id = documentSnapshot.getId();
            String path = documentSnapshot.getReference().getPath();
            Toast.makeText(AdminReadActivity.this,
                    "Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();

            String chapterName = adapter.getItem(position).getChapterName();
            String chapterInfo = adapter.getItem(position).getChapterInfo();
            Integer chapterNumber = adapter.getItem(position).getChapterNumber();

            Intent intent = new Intent(AdminReadActivity.this, AdminUpdateActivity.class);

            intent.putExtra("mChapterName", chapterName);
            intent.putExtra("mChapterInfo", chapterInfo);
            intent.putExtra("mChapterNumber", chapterNumber);
            intent.putExtra("mId", id); // Add this line

            startActivity(intent);

然后在您的UpdateActivity中获取ID(与获取mChapterName,mChapterInfo ...的方式相同)并在UpdateBook1()方法中使用此ID

private void updateBook1() {

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    DocumentReference bookRef = db.collection("Book").document(id); // ID got from the Intent Extras

}
© www.soinside.com 2019 - 2024. All rights reserved.