简体   繁体   中英

RecyclerView doesn't refresh after Firebase update

I have a RecyclerView and for each item, you can start the EditActivity (for Result) to update your text with Firebase.

The problem is that when you come back to the RecyclerView, data is not refreshed

Here's code from my adapter :

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();
            }
        }
    }

Here's code from my 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();
                }
                else{
                    String error = task.getException().getMessage();
                    Toast.makeText(EditActivity.this, R.string.error + error, Toast.LENGTH_LONG).show();
                }
                mProgress.setVisibility(View.INVISIBLE);
                edit_btn.setVisibility(View.VISIBLE);
            }
        });
    }
});

How to refresh the RecyclerView and setText with the new Text ?

I would be very grateful for your help :)

There are few things you're doing wrong.

  1. Pass the clickedItem position to EditActivity to update it later on.

     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); edit_intent.putExtra("position", position); ((Activity) context).startActivityForResult(edit_intent, 1); } }); 
  2. In EditActivity , Save the position in a variable & Return it back with new text

     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); //Return the position s well intent.putExtra("position", position); setResult(RESULT_OK, intent); finish(); } else{ String error = task.getException().getMessage(); Toast.makeText(EditActivity.this, R.string.error + error, Toast.LENGTH_LONG).show(); } mProgress.setVisibility(View.INVISIBLE); edit_btn.setVisibility(View.VISIBLE); } }); } }); 
  3. override onActivityResult() in your adaptor's parent activity

     public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if(resultCode == RESULT_OK){ String newText = data.getStringExtra("newText"); int itemPosition = data.getIntExtra("position"); //Pass these values to adapter through `updateItem` method adapter.updateItem(newText, itemPosition); } } } 
  4. Define the updateItem method inside adapter class

     public void updateItem(String newData, int index) { textList.set(index, newData); adapter.notifyItemChanged(index); } 

Cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM