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

时间:2019-04-25 16:15:55

标签: java android google-cloud-firestore

我正在使用Firebase Firestore进行更新。我目前可以在RecyclerView中单击一个位置,然后从那里获取相关信息到我的编辑文本框中。但是,我当前的问题是我正在努力选择正确的ID进行更新。我试图弄清楚如何在更新时将代码用于原始选择位置。

当前,当我单击该职位时,会有一个吐司显示器显示正确的ID和位置,然后将您带到我的更新活动,当我单击“保存”时,它将显示不正确的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)

1 个答案:

答案 0 :(得分: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

}