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

标签 java android google-cloud-firestore

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

目前,当我单击位置时,我有一个显示正确 ID 和位置的 toast 显示,然后它会将您带到我的更新 Activity ,当我单击保存时,它会显示不正确的 ID。我正在尝试让这个 ID 匹配,这样我就可以让我的更新正常工作。

我目前一直在尝试让代码正常工作,但没有成功。我所做的一件事是更改我的代码,以便在我单击更新时它会告诉我它正在选择哪个 ID。这样做会导致它选择了错误的 ID/随机 ID。这使我认为我的问题在于选择 ID。

阅读 Activity

这是我在 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);

编辑:更新 Activity

这是我的 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();
            }

        });
    }

图书适配器

 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 中单击“保存”时如何获取 ID,以显示我第一次从 RecyclerView 中选择它时显示的正确且相同的 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)

最佳答案

需要通过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

}

关于java - 选择要更新的位置时如何正确使用Firestore ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853616/

相关文章:

android - <android.support.design.widget.TextInputEditText> 和 <EditText> 之间的区别

android - 软键盘进来后,如何保持部分组件固定?

angular - 'array-contains' 查询不适用于引用的 Firestore

java - 为什么我们在向firestore发送数据时,将HashMap中的参数设置为object?

java - 用特殊字符替换 & 符号的正则表达式模式

java - 使用自定义 rgb 对列的单元格进行着色,以在列中创建渐变图案,每个单元格使用 apache poi 都有自己的 rgb

java - 使用 Tomcat 设置本地开发环境时出现 HTTP 500 错误

java - 每 2 小时在 n 到 m 范围内的随机分钟触发一次 Jenkins 作业

android - 如何使cardview中的imageview左对齐

javascript - QueryDocumentSnapshot 似乎缺少其父类的方法