1 回答
TA贡献2039条经验 获得超7个赞
你做错的事情很少。
将 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);
}
干杯 :)
添加回答
举报