如何在Firestore RecyclerView中更新项目?

时间:2019-04-07 21:58:40

标签: java android firebase android-recyclerview google-cloud-firestore

我正在尝试通过工具栏中的保存按钮更新RecyclerViewupdateQuestion()上的项目。

我的Firebase中具有以下结构:

Questions

-> list_of_documents wUniqueIDs

--->文档中的字段:

+ question

+ answer

+ rating

到目前为止,我在updateQuestion()下有这个名字

private void updateQuestion() {
        String questionString = mQuestionTextView.getText().toString();
        Object updatedText = mQuestionEditText.getText().toString();
        String questionPath2 = rootRef.collection("Questions").document().getPath();

        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();

        CollectionReference collectionReference = rootRef.collection(questionPath2).document().update("question", updatedText); // <--- I GET ERROR HERE
}
error: incompatible types: Task<Void> cannot be converted to CollectionReference

我之前也遇到一个错误,说update()的第二个参数必须是一个对象,但是这个错误似乎已经消失了。

我一直在尝试按照教程学习,并在stackOverflow上查看其他帖子,但似乎都没有解决这个问题。

这是我以前尝试过的:

//        CollectionReference ref = rootRef.collection("Questions").document().collection("question");
//        CollectionReference ref = rootRef.collection("Questions").document().collection(questionPath);

//        Toast.makeText(this, "this is the path: " + questionPath, Toast.LENGTH_SHORT).show();
//        Log.d(TAG, "updateQuestion: " + ref.get(question.getAnswer));
//        Log.d(TAG, "updateQuestion: " + ref.collection(questionPath).document("question", questionPath).toString())
//        Log.d(TAG, "updateQuestion: " + ref.document(questionPath)("question").toString());
//        Log.d(TAG, "updateQuestion: " + ref.document(questionPath).get(question).toString());
//        ref.collection("question")
//        db.child("Spacecraft").push().setValue(spacecraft);
//        rootRef.collection("Questions").document().update("question", updatedText);
//        rootRef.collection("Questions").document(questionPath).update("question", updatedText);

//        TextView savedText = mQuestionTextView.setText(updatedText);

//        DocumentReference documentReference = rootRef.collection("Questions").document(questionPath).update("question", );
//        DocumentReference documentReference = rootRef.document(questionPath).update("question", updatedText);
//        CollectionReference collectionReference = rootRef.collection(questionPath).document().update("question", updatedText);

感谢您的帮助。

------------------ 截至2019年4月7日@ 1153pm的修订代码 ---------

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.save_question:
                mShowSaveIcon = false;
                updateQuestion();
                break;

            case R.id.edit_question:
                item.setVisible(false);
                enableEditMode();
                mShowSaveIcon = true;
                break;

        }
        invalidateOptionsMenu();
        return true;
    }

    private void enableEditMode(){

        mQuestionEditText = findViewById(R.id.questionEditTextID);
        mPostAnswerButton.setEnabled(false);
        mPostAnswerButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

        mCommentButton.setEnabled(false);
        mCommentButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

        String questionString = mQuestionTextView.getText().toString();
        mQuestionTextView.setVisibility(View.GONE);
        mQuestionEditText.setVisibility(View.VISIBLE);
        mQuestionEditText.setText(questionString);
    }



    private void updateQuestion(){
        Question question = createQuestionObj();
        updateQuestionToCollection(question);
    }

    private Question createQuestionObj(){
        final Question question = new Question();
        return question;
    };

private void updateQuestionToCollection(Question question) {
        String questionString = mQuestionTextView.getText().toString();
        Object updatedText = mQuestionEditText.getText().toString();


        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        String docId = rootRef.collection("Questions").document().getId();
        System.out.println(docId);
        String questionPath = rootRef.collection("Questions").document(docId).collection("question").getPath();
//        String questionPath2 = rootRef.collection("Questions").document(docId).getPath();

        System.out.println(questionPath);
        rootRef.collection(questionPath).document(docId).update("question", updatedText).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "onSuccess: it worked");
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "onFailure: it failed because of: " + e.toString());
            }
        });

finish();
 }
};

下面的数据库结构代码段

enter image description here

1 个答案:

答案 0 :(得分:1)

当我查看您的代码时,我看到两个问题。第一个是以下错误:

error: incompatible types: Task<Void> cannot be converted to CollectionReference

之所以这样产生,是因为当您在update()对象上调用DocumentReference方法时,返回的对象是Task<Void>类型的,而不是 {{ 1}}。另请注意,Java中无法将CollectionReference对象强制转换为Task。因此,要解决此问题,只需删除CollectionReference对象的声明,如下所示:

collectionReference

第二个问题是,每次调用rootRef.collection(questionPath2).document("specificDocId").update("question", updatedText); 时都会生成一个新的文档ID,因为您没有将任何ID传递给updateQuestion()方法。为了能够更新特定的文档,您需要传递文档的实际ID,以便可以对其进行更新:

document()

编辑:

要更新文档,请使用以下参考:

rootRef.collection(questionPath2).document("specificDocId").update("question", updatedText);