这是一个复杂的问题。在我的RecyclerView的自定义适配器的ViewHolder
中,我已经实现了onClickListener
并通过onClick
,我正在通过文本和recyclerview传递EditItemActivity
的意图将项目位置列为附加项目。在EditItemActivity
中,我有一个TextView,其中包含从intent传递的文本和“Save Changes”按钮。该文本属于我的Recyclerview列表中的特定列表项。在我更改文本后,我希望应用程序返回到包含列表的主屏幕,并使用新文本更新该特定项目。
到目前为止,我能够成功实施:
EditItemActivity
就会启动。我要添加的内容:
EditItemActivity
中的文字并单击“保存更改”按钮时,它会更新同一列表项的文本。这是我的实施。我的适配器中的ViewHolder
包含onClick
:
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 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");
final String position = getIntent().getExtras().getString("position");
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();
}
}
});
}
}
根据要求添加代码
具有listview的类的代码:
public class MainActivity extends AppCompatActivity {
private List<ToDoData> toDoList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToDoAdapter toDoAdapter = new ToDoAdapter(toDoList, this);
final EditText toDoInput = (EditText)findViewById(R.id.add_todo);
Button toDoAdd = (Button)findViewById(R.id.add_item);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView toDoDisplay = (RecyclerView) findViewById(R.id.toDoDisplayRecyclerView);
toDoDisplay.setAdapter(toDoAdapter);
toDoDisplay.setHasFixedSize(true);
toDoDisplay.setLayoutManager(layoutManager);
toDoAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ToDoData newToDoData = new ToDoData(MainActivity.this);
newToDoData.setToDo(toDoInput.getText().toString());
toDoList.add(newToDoData);
toDoAdapter.notifyDataSetChanged();
toDoInput.setText("");
}
});
}
}
适配器代码:
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();
}
}
ToDoData.class
public class ToDoData implements Serializable{
private String toDoString;
public ToDoData(String todoString){
this.toDoString = todoString;
}
public ToDoData(Context context){}
public String getToDo() {
return toDoString;
}
public void setToDo(String toDoString) {
this.toDoString = toDoString;
}
}
答案 0 :(得分:0)
使您的ToDoData类实现Serializable。
public class ToDoData implements Serializable{}
现在,将List<ToDoData>
与positon
和data
一起传递给EditItemActivity,并通过Intent传递:
Intent intent = new Intent(MainActivity.this, EditItemActivity.class);
intent.putExtra("data", todoView.getText());
intent.putExtra("position", getAdapterPosition());
//along with data and position, we are passing the list as well.
intent.putExtra("LIST", (Serializable) toDoList); //this 'list' is your toDoList that you pass to the adapter (and gets populated into the view)
startActivity(intent);
现在,在EditItemActivity中,
从意图中接收列表:
Intent i = getIntent();
list = (List<ToDoData>) i.getSerializableExtra("LIST");
并将按钮单击的侦听器设置为
toDoAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// set the new input of user from toDoInput into the list
list.set(position, *object_of_ToDoData_with_updated_user_input*);
startActivity(new Intent(EditItemActiviy.this, MainActivity.class));
}
});
在MainActivity中,
@Override
protected void onResume() {
super.onResume();
toDoAdapter.notifyDataSetChanged();
}