已解决 - 答案位于主题
的末尾我正在创建一个笔记应用。在那个应用程序中,到目前为止一切进展顺利,只是当我尝试编辑注释时(在回收器视图中)并且我单击保存,它创建一个新的而不是重新保存现有注释的内容< / strong>即可。因为我没有办法再次保存它,因为我不知道如何去做。以下是我在活动中保存注释的方法, CreateNoteActivity.java
// FAB
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check to see if at least one field is populated with data
String title = etTitle.getText().toString();
String description = etDescription.getText().toString();
title = title.trim(); // Remove whitespaces at the beginning/end
description = description.trim();
// Get intent extras
Intent intent = getIntent();
String alreadyCreatedTitle = intent.getStringExtra(CreateNoteActivity.EXTRA_TITLE);
String alreadyCreatedDescription = intent.getStringExtra(CreateNoteActivity.EXTRA_DESCRIPTION);
// Check to see if note title is empty, if it is, don't save
if (title == "" || title.isEmpty()) {
Snackbar snackbar = Snackbar.make(view, "Title may not be empty", Snackbar.LENGTH_SHORT);
snackbar.show();
// If the user clicked an already made note and did not change its contents, go back to MainActivity
} else if (title.equals(alreadyCreatedTitle) && description.equals(alreadyCreatedDescription)) {
finish();
CreateNoteActivity.didClick = false;
// The user is editing a note
} else if (didClick) {
// If the title or description is different then resave the note
if (!title.equals(alreadyCreatedTitle) || !description.equals(alreadyCreatedDescription)) {
// TODO: Make it resave the note object
}
} else {
saveNote();
CreateNoteActivity.didClick = false;
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
之后,它来到我的MainActivity.java类,其中onActivityResult()处理数据并保存新笔记,或更新现有笔记(尚未实现)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 123) {
if (resultCode == Activity.RESULT_OK) {
String passedTitle = data.getStringExtra(CreateNoteActivity.EXTRA_TITLE);
String passedDescription = data.getStringExtra(CreateNoteActivity.EXTRA_DESCRIPTION);
if (CreateNoteActivity.didClick) { // User is saving an existing note
// TODO: Resave the existing note object
// **************************
} else { // User is creating a new note
notes.add(new Note(passedTitle, passedDescription));
}
}
refreshAdapter();
if (resultCode == Activity.RESULT_CANCELED) {
// Do something if it's cancelled. Happens when you click the back button for example
}
}
}
正如你所看到的,我在这个类中有一个变量“CreateNoteActivity.didClick = false;”,我用它来跟踪用户是否点击了一个音符,它让他们参加了这项活动。
我在我的适配器类中跟踪它
@Override
public void onClick(View view) {
CreateNoteActivity.didClick = true;
Log.d("TAG", "onClick() called on row: " + getAdapterPosition());
Intent intent = new Intent(context, CreateNoteActivity.class);
intent.putExtra(CreateNoteActivity.EXTRA_TITLE, titleTV.getText().toString());
intent.putExtra(CreateNoteActivity.EXTRA_DESCRIPTION, descriptionTV.getText().toString());
((Activity) context).startActivityForResult(intent, 123);
}
因此,当用户单击特定索引处的注释时,它会将意图附加内容传递给CreateNoteActivity.java,以便它可以检索它们,并使用其信息填充编辑文本。所以现在我想要做的是,如果用户点击保存,它不会创建新笔记,而是重新保存旧笔记
我真的很感激任何人帮助和反馈解决这个问题。现在被困在它上几个小时,我只是不知道如何绕过去做这件事。非常感谢你。
我的简单笔记课
public class Note {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Note(String title, String description) {
this.title = title;
this.description = description;
}
public Note() {
// Empty
}
}
解决方案
所以我最终做的是创建一个静态int并将其设置为getAdapterPosition(),这样我总能得到某个对象所处的位置。然后我将它作为一个额外的意图传递并检索它,以便我可以搞砸它。在指定的索引处删除它,并在该索引处设置一个新索引。
if (CreateNoteActivity.didClick) { // User is saving an existing note
note.setTitle(passedTitle);
note.setDescription(passedDescription);
notes.remove(passedID); // Remove note so I can put that same one at the top
notes.add(0, note); // Put note at top of list
CreateNoteActivity.didClick = false;
recyclerView.scrollToPosition(0); // Scroll to top
} else { // User is creating a new note
note.setTitle(passedTitle);
note.setDescription(passedDescription);
notes.add(0, note);
recyclerView.scrollToPosition(0);
}
}
答案 0 :(得分:1)
您还应该存储id
的{{1}}并传递它以便能够进行编辑。您可以将其位置用作Note
。
id