我正在尝试在Android中制作笔记/日记录入应用程序。我希望更新条目时数据库应占用系统时间,然后将其添加到列表视图中。
list_personal.xml <
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="1."
android:id="@+id/index_personal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="@+id/title_personal"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_marginTop="5dp"
android:text="Description"
android:id="@+id/description_personal"/>
</LinearLayout>
</LinearLayout>
DatabaseHandler_personal.class
package dbms.vit.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHandler_personal extends SQLiteOpenHelper {
public DatabaseHandler_personal(Context context) {
super(context, DatabaseValues_personal.DATABASE_NAME, null, DatabaseValues_personal.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { db.execSQL(DatabaseValues_personal.TABLE_NOTES_PERSONAL_CREATE);}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DatabaseValues_personal.TABLE_NOTES_PERSONAL_DROP);
onCreate(db);
}
public void addNote(Note note){
SQLiteDatabase db_personal = this.getWritableDatabase();
ContentValues noteValues = new ContentValues();
noteValues.put(DatabaseValues_personal.NOTES_TITLE_PERSONAL, note.getTitle());
noteValues.put(DatabaseValues_personal.NOTES_DESCRIPTION_PERSONAL, note.getDescription());
db_personal.insert(DatabaseValues_personal.TABLE_NOTES_PERSONAL, null, noteValues);
db_personal.close();
}
public void updateNote(Note note){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues noteValues = new ContentValues();
noteValues.put(DatabaseValues_personal.NOTES_TITLE_PERSONAL, note.getTitle());
noteValues.put(DatabaseValues_personal.NOTES_DESCRIPTION_PERSONAL, note.getDescription());
db.update(DatabaseValues_personal.TABLE_NOTES_PERSONAL, noteValues, DatabaseValues_personal.NOTES_ID_PERSONAL + " = ?", new String[]{String.valueOf(note.getId())});
db.close();
}
public void deleteNote(String id){
SQLiteDatabase db = this.getWritableDatabase();
String deletequery= "DELETE FROM " + DatabaseValues_personal.TABLE_NOTES_PERSONAL + " WHERE " + DatabaseValues_personal.NOTES_ID_PERSONAL + " = '" + id + "'";
db.execSQL(deletequery);
db.close();
}
public List<Note> getAllNotes(){
List<Note> notes = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery= "SELECT * FROM " + DatabaseValues_personal.TABLE_NOTES_PERSONAL;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do{
Note note = new Note();
note.setId(Integer.parseInt(cursor.getString(0)));
note.setTitle(cursor.getString(1));
note.setDescription(cursor.getString(2));
notes.add(note);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return notes;
}
}
DatabaseValues_personal.class
package dbms.vit.myapplication;
public class DatabaseValues_personal {
public static final int DATABASE_VERSION = 13;
public static final String DATABASE_NAME = "personal_note";
public static final String TABLE_NOTES_PERSONAL = "personal_notes";
public static final String NOTES_ID_PERSONAL = "id";
public static final String NOTES_TITLE_PERSONAL = "title_personal";
public static final String NOTES_DESCRIPTION_PERSONAL = "description_personal";
public static final String NOTES_DATE_PERSONAL = "data_personal";
public static final String TABLE_NOTES_PERSONAL_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NOTES_PERSONAL + "( " + NOTES_ID_PERSONAL + " INTEGER PRIMARY KEY, " + NOTES_TITLE_PERSONAL + " TEXT, " + NOTES_DESCRIPTION_PERSONAL + " TEXT, " + NOTES_DATE_PERSONAL + " INTEGER NOT NULL DEFAULT (strftime('%s','now')) ";
public static final String TABLE_NOTES_PERSONAL_DROP = "DROP TABLE IF EXISTS " + TABLE_NOTES_PERSONAL;
}