android编写笔记app的代码
时间: 2024-01-21 08:03:42 浏览: 224
android 我的笔记 源码
4星 · 用户满意度95%
这个问题比较广泛,需要分步骤讲解。以下是一些可能的步骤和代码示例:
1. 创建一个笔记类(Note):
```java
public class Note {
private int id;
private String title;
private String content;
private Date createdDate;
private Date modifiedDate;
public Note() {
// 构造函数
}
// getter 和 setter 方法
// 重写 toString 方法,方便调试
@Override
public String toString() {
return "Note{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", createdDate=" + createdDate +
", modifiedDate=" + modifiedDate +
'}';
}
}
```
2. 创建一个数据库帮助类(DbHelper):
```java
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "notes.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "notes";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_CONTENT = "content";
private static final String COLUMN_CREATED_DATE = "created_date";
private static final String COLUMN_MODIFIED_DATE = "modified_date";
// SQL 语句
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY," +
COLUMN_TITLE + " TEXT," +
COLUMN_CONTENT + " TEXT," +
COLUMN_CREATED_DATE + " INTEGER," +
COLUMN_MODIFIED_DATE + " INTEGER)";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 数据库升级
}
}
```
3. 创建一个数据访问层(NoteDao):
```java
public class NoteDao {
private SQLiteDatabase db;
private DbHelper dbHelper;
public NoteDao(Context context) {
dbHelper = new DbHelper(context);
db = dbHelper.getWritableDatabase();
}
public long add(Note note) {
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_TITLE, note.getTitle());
values.put(DbHelper.COLUMN_CONTENT, note.getContent());
values.put(DbHelper.COLUMN_CREATED_DATE, note.getCreatedDate().getTime());
values.put(DbHelper.COLUMN_MODIFIED_DATE, note.getModifiedDate().getTime());
return db.insert(DbHelper.TABLE_NAME, null, values);
}
public int update(Note note) {
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_TITLE, note.getTitle());
values.put(DbHelper.COLUMN_CONTENT, note.getContent());
values.put(DbHelper.COLUMN_MODIFIED_DATE, note.getModifiedDate().getTime());
String whereClause = DbHelper.COLUMN_ID + "=?";
String[] whereArgs = { String.valueOf(note.getId()) };
return db.update(DbHelper.TABLE_NAME, values, whereClause, whereArgs);
}
public int delete(Note note) {
String whereClause = DbHelper.COLUMN_ID + "=?";
String[] whereArgs = { String.valueOf(note.getId()) };
return db.delete(DbHelper.TABLE_NAME, whereClause, whereArgs);
}
public List<Note> getAll() {
List<Note> notes = new ArrayList<>();
Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null,
DbHelper.COLUMN_MODIFIED_DATE + " DESC");
while (cursor.moveToNext()) {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(DbHelper.COLUMN_ID)));
note.setTitle(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_TITLE)));
note.setContent(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_CONTENT)));
note.setCreatedDate(new Date(cursor.getLong(cursor.getColumnIndex(DbHelper.COLUMN_CREATED_DATE))));
note.setModifiedDate(new Date(cursor.getLong(cursor.getColumnIndex(DbHelper.COLUMN_MODIFIED_DATE))));
notes.add(note);
}
cursor.close();
return notes;
}
}
```
4. 创建一个笔记列表界面(NoteListActivity):
```java
public class NoteListActivity extends AppCompatActivity {
private static final int REQUEST_CODE_ADD = 1;
private static final int REQUEST_CODE_EDIT = 2;
private ListView listView;
private List<Note> notes;
private NoteDao noteDao;
private ArrayAdapter<Note> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_list);
noteDao = new NoteDao(this);
listView = findViewById(R.id.list_view);
notes = noteDao.getAll();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = notes.get(position);
Intent intent = new Intent(NoteListActivity.this, NoteEditActivity.class);
intent.putExtra("note", note);
startActivityForResult(intent, REQUEST_CODE_EDIT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_ADD:
Note note = (Note) data.getSerializableExtra("note");
long id = noteDao.add(note);
note.setId((int) id);
notes.add(0, note);
adapter.notifyDataSetChanged();
break;
case REQUEST_CODE_EDIT:
note = (Note) data.getSerializableExtra("note");
noteDao.update(note);
adapter.notifyDataSetChanged();
break;
}
}
}
public void onAddButtonClick(View view) {
Intent intent = new Intent(this, NoteEditActivity.class);
startActivityForResult(intent, REQUEST_CODE_ADD);
}
}
```
5. 创建一个笔记编辑界面(NoteEditActivity):
```java
public class NoteEditActivity extends AppCompatActivity {
private EditText titleEditText;
private EditText contentEditText;
private Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_edit);
titleEditText = findViewById(R.id.title_edit_text);
contentEditText = findViewById(R.id.content_edit_text);
note = (Note) getIntent().getSerializableExtra("note");
if (note == null) {
note = new Note();
} else {
titleEditText.setText(note.getTitle());
contentEditText.setText(note.getContent());
}
}
public void onSaveButtonClick(View view) {
note.setTitle(titleEditText.getText().toString());
note.setContent(contentEditText.getText().toString());
note.setModifiedDate(new Date());
Intent intent = new Intent();
intent.putExtra("note", note);
setResult(RESULT_OK, intent);
finish();
}
}
```
这些代码只是一个简单的示例,实际开发中需要根据具体需求进行修改和扩展。
阅读全文