我正在建立一个应用程序,我正在尝试添加编辑功能。单击某个项目时,用户需要EditItemActivity.java
,用户可以在其中编辑该特定内容。例如,它可以从" Do Homework"到"去健身房"。
到目前为止,我已经能够获取被点击的项目并将其文本显示在将用于编辑的EditText字段中。但是,当单击按钮并返回主屏幕时,我无法弄清楚如何保存编辑。
所以在我的适配器中,我用额外的方式调用了意图:
public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.ViewHolder> {
private List<ToDoData> toDoList;
private Context context;
public ToDoAdapter(List<ToDoData> todoList, Context context) {
this.toDoList = todoList;
this.context = context;
}
public ToDoAdapter(Context context){}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView todoView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
todoView = itemView.findViewById(R.id.to_do_display);
}
@Override
public void onClick(View view) {
Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, EditItemActivity.class);
intent.putExtra("data", todoView.getText());
intent.putExtra("position", getAdapterPosition());
context.startActivity(intent);
}
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ToDoData todoPosition = toDoList.get(position);
holder.todoView.setText(todoPosition.getToDo());
}
@Override
public int getItemCount() {
return (toDoList == null) ? 0 : toDoList.size();
}
}
在我的EditItemActivity.java
中,我正在检索文本并将其放在我的EditText字段中:
public class EditItemActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
final ToDoData toDoData = new ToDoData(this);
final ToDoAdapter toDoAdapter = new ToDoAdapter(this);
final EditText editToDo = (EditText)findViewById(R.id.edit_todo_item);
Button button = (Button)findViewById(R.id.save_changes);
final String text = getIntent().getExtras().getString("data");
editToDo.setText(text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(editToDo.getText().toString() != text){
toDoData.setToDo(editToDo.getText().toString());
toDoAdapter.notifyDataSetChanged();
finish();
}
}
});
}
}
但不幸的是,我不知道怎么走得更远。
答案 0 :(得分:2)
你要做的是:
1-发送视图位置以编辑活动
@Override
public void onClick(View view) {
Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, EditItemActivity.class);
intent.putExtra("data", todoView.getText());
intent.putExtra("position", getAdapterPosition());
context.startActivity(intent);
}
2-编辑活动
final String position= getIntent().getExtras().getString("position");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new intent(this, [PREVIOUS_ACTIVITY]);
intent.putExtra("new data", todoView.getText().toString());
intent.putExtra("position", position);
startActivity(intent);
}
});
4 - 在包含适配器创建散列映射的活动中将即将到来的额外内容(位置 - 文本)保存为(键 - 值)
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(position, newText);
5-然后在创建实例时将此哈希映射传递给适配器类。并且在onBindViewHolder()
中检查即将到来的视图位置是否包含在hashmap中,如果是,则设置新数据
if(hashMap.containsKey(position))
view.todoView.setText(hashMap.get(position));