我有一个RecyclerView,对于每个项目,您都可以启动EditActivity(用于Result)以使用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();
}
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);
}
});
}
});
如何使用新的Text刷新RecyclerView和setText?
我将非常感谢您的帮助:)
最佳答案
您做错的事情很少。
将clickedItem位置传递给EditActivity
以便以后进行更新。
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);
}
});
在
EditActivity
中,将位置保存在变量中并用新文本返回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);
}
});
}
});
覆盖适配器的父活动中的
onActivityResult()
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);
}
}
}
在适配器类中定义
updateItem
方法public void updateItem(String newData, int index)
{
textList.set(index, newData);
adapter.notifyItemChanged(index);
}
干杯:)