Ad
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 :)
Ad
Answer
There are few things you're doing wrong.
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); } });
In
EditActivity
, Save the position in a variable & Return it back with new textsetOnClickListener(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); } }); } });
override
onActivityResult()
in your adaptor's parent activitypublic 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); } } }
Define the
updateItem
method inside adapter classpublic void updateItem(String newData, int index) { textList.set(index, newData); adapter.notifyItemChanged(index); }
Cheers :)
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad