基本问题是,在我的笔记应用程序(https://www.youtube.com/watch?v=48EB4HeP1kI&list=LLhpPM8FLd5uST7RfQa_riQg&index=3&t=0s)中,创建新笔记会创建与先前笔记相同的保存笔记。
我修改了代码,以便存储的注释格式不是一个大的编辑Text
,而是一个值表。
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), TableNoteEditorActivity.class);
intent.putExtra("noteId", i);
startActivity(intent);
}
});
这与表格中的其他NoteEditorClass
Intent intent = getIntent();
noteId = intent.getIntExtra("noteId", -1);
if(noteId != -1){
editText.setText(MainActivity.notes.get(noteId));
} else {
MainActivity.notes.add("");
noteId = MainActivity.notes.size() - 1;
MainActivity.arrayAdapter.notifyDataSetChanged();
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
MainActivity.notes.set(noteId, String.valueOf(charSequence));
MainActivity.arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.NoteLab", Context.MODE_PRIVATE);
HashSet<String> set = new HashSet(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
我认为noteId
与区分不同的音符有关。很抱歉这么笨我仅在Comp Sci课程中学习了Java的基础知识。预先感谢!