gpt4 book ai didi

java - 重新保存对象而不是在列表中创建新对象

转载 作者:行者123 更新时间:2023-12-01 10:08:01 25 4
gpt4 key购买 nike

已解决 - 答案在线程末尾

我正在创建一个笔记应用程序。在该应用程序中,到目前为止一切进展顺利,除了当我尝试编辑注释(在回收器 View 中)并单击“保存”时,它会创建一个新注释,而不是重新保存现有注释的内容 。因为我没有办法再次保存它,因为我不知道如何去做。以下是我在 Activity 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
}
}
}

正如你所看到的,我在这个类中有一个静态 boolean 变量“CreateNoteActivity.didClick = false;”,我用它来跟踪用户是否单击了注释,并且它让他们参与了这项 Activity 。

我在我的适配器类中跟踪它

    @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);
}

因此,当用户单击特定索引处的注释时,它会将 Intent 附加信息传递给 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
}


}

解决方案

所以我最终做的是创建一个 static int 并将其设置为 getAdapterPosition() ,以便我始终可以获得某个对象所在的位置。然后我将它作为一个 Intent 额外传递并检索它,以便我可以处理它。在该指定索引处将其删除,并在该索引处设置一个新索引。

        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);
}
}

最佳答案

您还应该存储 Noteid 并传递它以便能够进行编辑。您可以使用其位置作为 id

if (CreateNoteActivity.didClick) { // User is saving an existing note
Note anote = notes.getItem(passedNoteId);
anote.setTitle(passedTitle);
anote.setDescription(passedDescription);
} else { // User is creating a new note
notes.add(new Note(passedTitle, passedDescription));
}

关于java - 重新保存对象而不是在列表中创建新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com