基于Android平台实现简单的个人笔记管理的APP 代码
时间: 2023-12-22 11:04:58 浏览: 128
很抱歉,我无法在这里提供完整的代码。但是,我可以为你提供一些代码示例以供参考。
以下是一个简单的笔记管理APP的代码示例,其中包括笔记的添加、编辑、删除、分类和标签等功能:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NoteAdapter noteAdapter;
private List<Note> noteList;
private FloatingActionButton addNoteButton;
private EditText noteTitleEditText, noteContentEditText;
private Spinner categorySpinner, tagSpinner;
private Button saveNoteButton, cancelNoteButton;
private NoteDatabase noteDatabase;
private int noteId = -1;
private List<String> categoryList, tagList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
noteList = new ArrayList<>();
noteAdapter = new NoteAdapter(this, noteList);
recyclerView.setAdapter(noteAdapter);
addNoteButton = findViewById(R.id.add_note_button);
addNoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNoteDialog();
}
});
noteDatabase = new NoteDatabase(this);
loadNotes();
}
private void showNoteDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.note_dialog, null);
dialogBuilder.setView(dialogView);
noteTitleEditText = dialogView.findViewById(R.id.note_title_edit_text);
noteContentEditText = dialogView.findViewById(R.id.note_content_edit_text);
categorySpinner = dialogView.findViewById(R.id.category_spinner);
tagSpinner = dialogView.findViewById(R.id.tag_spinner);
saveNoteButton = dialogView.findViewById(R.id.save_note_button);
cancelNoteButton = dialogView.findViewById(R.id.cancel_note_button);
categoryList = loadCategories();
ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, categoryList);
categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(categoryAdapter);
tagList = loadTags();
ArrayAdapter<String> tagAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, tagList);
tagAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tagSpinner.setAdapter(tagAdapter);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
saveNoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
String category = categorySpinner.getSelectedItem().toString();
String tag = tagSpinner.getSelectedItem().toString();
if (noteId == -1) {
Note note = new Note(title, content, category, tag);
noteDatabase.addNote(note);
noteList.add(note);
} else {
Note note = noteList.get(noteId);
note.setTitle(title);
note.setContent(content);
note.setCategory(category);
note.setTag(tag);
noteDatabase.updateNote(note);
noteId = -1;
}
noteAdapter.notifyDataSetChanged();
alertDialog.dismiss();
}
});
cancelNoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
private void loadNotes() {
noteList.clear();
noteList.addAll(noteDatabase.getAllNotes());
noteAdapter.notifyDataSetChanged();
}
private List<String> loadCategories() {
List<String> categories = new ArrayList<>();
categories.add("Personal");
categories.add("Work");
categories.add("Study");
return categories;
}
private List<String> loadTags() {
List<String> tags = new ArrayList<>();
tags.add("Important");
tags.add("Urgent");
tags.add("ToDo");
return tags;
}
public void editNote(int position) {
Note note = noteList.get(position);
noteId = position;
noteTitleEditText.setText(note.getTitle());
noteContentEditText.setText(note.getContent());
categorySpinner.setSelection(categoryList.indexOf(note.getCategory()));
tagSpinner.setSelection(tagList.indexOf(note.getTag()));
showNoteDialog();
}
public void deleteNote(int position) {
Note note = noteList.get(position);
noteDatabase.deleteNoteById(note.getId());
noteList.remove(position);
noteAdapter.notifyDataSetChanged();
}
}
```
以上代码仅为示例,可能存在一些问题,需要根据具体情况进行修改和完善。希望能对你有所帮助!
阅读全文