猿问

Firebase 更新后 RecyclerView 不刷新

我有一个 RecyclerView,对于每个项目,您可以启动 EditActivity(对于结果)以使用 Firebase 更新您的文本。


问题是当你回到 RecyclerView 时,数据没有刷新


这是我的适配器的代码:


holder.editBtn.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    Intent edit_intent = new Intent(holder.authorText.getContext(), EditActivity.class);

                    edit_intent.putExtra("text", textList.get(position).getBody());

                    edit_intent.putExtra("id", textList.get(position).textId);

                    ((Activity) context).startActivityForResult(edit_intent, 1);

                }

            });


 public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {

            if(resultCode == RESULT_OK){

                notifyDataSetChanged();

            }

        }

    }

这是我的 EditActivity 中的代码:


edit_btn.setOnClickListener(new View.OnClickListener() {


    @Override

    public void onClick(View v) {

        mProgress.setVisibility(View.VISIBLE);

        edit_btn.setVisibility(View.INVISIBLE);

        String new_text = edit_text.getText().toString();

        mFirestore.collection("Text").document(text_id).update("body", new_text).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override

            public void onComplete(@NonNull Task<Void> task) {

                if(task.isSuccessful()){

                    Toast.makeText(EditActivity.this, R.string.changes, Toast.LENGTH_LONG).show();

                    Intent intent = new Intent();

                    intent.putExtra("newText", new_text);

                    setResult(RESULT_OK, intent);

                    finish();

                }

            }

        });

    }

});


如何用新的 Text 刷新 RecyclerView 和 setText ?


慕斯王
浏览 143回答 1
1回答

largeQ

你做错的事情很少。将 clickedItem 位置传递给以EditActivity稍后更新它。holder.editBtn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent edit_intent = new Intent(holder.authorText.getContext(), EditActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edit_intent.putExtra("text", textList.get(position).getBody());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edit_intent.putExtra("id", textList.get(position).textId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edit_intent.putExtra("position", position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((Activity) context).startActivityForResult(edit_intent, 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });在 中EditActivity,将位置保存在变量中并用新文本返回setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgress.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edit_btn.setVisibility(View.INVISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String new_text = edit_text.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mFirestore.collection("Text").document(text_id).update("body", new_text).addOnCompleteListener(new OnCompleteListener<Void>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<Void> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(task.isSuccessful()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(EditActivity.this, R.string.changes, Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("newText", new_text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Return the position s well&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("position", position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setResult(RESULT_OK, intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String error = task.getException().getMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(EditActivity.this, R.string.error + error, Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mProgress.setVisibility(View.INVISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edit_btn.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });onActivityResult()在适配器的父活动中覆盖public void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; if (requestCode == 1) {&nbsp; &nbsp; &nbsp; &nbsp; if(resultCode == RESULT_OK){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String newText = data.getStringExtra("newText");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int itemPosition = data.getIntExtra("position");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Pass these values to adapter through `updateItem` method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.updateItem(newText, itemPosition);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}updateItem在适配器类中定义方法public void updateItem(String newData, int index){&nbsp; textList.set(index, newData);&nbsp; adapter.notifyItemChanged(index);}干杯 :)
随时随地看视频慕课网APP

相关分类

Java
我要回答