[アンドロイド] データベースtrongのアンドロイド - P4アップデートデータベース
チュートリアルの主な目的は、今日のAndroid内のすべてのデータベースです – 失われていないどのように適用するか、データベースを更新, 故障していないあなたは、データベースを変更する場合.
リーグの歴史の後に行われ、アプリの注意, あなたが最終修正時刻でメモをしたいです. だから我々は、単独で実行行くに向けます.
コンテンツ

ステップ 1: クラス構造は、注を変更します
各ノートには、最終的な改正のためのより多くの時間を持っている必要があります, 私たちは、クラスのノートに属性を追加する必要がありますLASTMODIFIED.
package cachhoc.net.tut.demodatabase;
public class Note {
private long id;
private String title;
private String content;
private String lastModified;
/**
* here is getter and setter of id, title, content.
*/
public String getLastModified() {
return lastModified;
}
public Note setLastModified(String lastModified) {
this.lastModified = lastModified;
return this;
}
}
私達はちょうど最終更新時刻を表示する必要があるが、それはdate型を使用せずに文字列と一緒に使用すべきかを計算する必要はありません (取り扱いがより困難になります日付タイプ).
ステップ 2: 更新DatabaseHelper
これは最も重要なステップです. 私たちは順番にDatabaseHelperの以下の部分を変更します:
コマンドテーブルの作成を変更します
まず、1の定数を追加する必要が新しい列のタイトルです. そして、テーブルのメモを作成するコマンドに追加
/**
* table note contain id, title, content
*/
public static final String TABLE_NOTE = "tb_note";
public static final String KEY_ID_NOTE = "id";
public static final String KEY_TITLE_NOTE = "title";
public static final String KEY_CONTENT_NOTE = "content";
public static final String KEY_LAST_MODIFIED_NOTE = "last_modified";
/**
* string for create table note
*/
public static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
"," + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'" +
")";
これは、新しい携帯電話はまた、表の列LAST_MODIFIEDノートにインストールすることが保証されます.
更新データベースのバージョン
あなたは、私たちが持っていた他の日を覚えています 1 変数ですDATA_VERSION, それがバージョンデータベースでした. 今、あなたは、以前のバージョンよりも大きいに成長します (最後の時間でした 1, 時間に増やす必要があります 2).
/** * value for update database */ public static final int DATA_VERSION = 2;
データベースの更新を行います
ONUPGRADEメソッドが呼び出されます前に、私は一日に言及していたとして、我々は、データベースのバージョンを更新するとき. ただDATA_VERSION変化は、このメソッドが呼び出されます. 次のように我々は、このメソッドを書き換えます:
/**
* call when change DATA_VERSION
* help we update database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// update database for database version < 2
if (oldVersion < 2) {
db.execSQL("ALTER TABLE " + TABLE_NOTE + " ADD COLUMN " + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'");
}
}
あなたは以下の点に気づきました:
- KHI更新アプリ, システムは自動的に上記の変数に古いバージョンのデータベースのバージョンを保存しますoldVersion. NEWVERSIONは新しいDATA_VERSIONです.
-
皆さんの中には、多くの場合、こののonCreateのテーブルとコールバック関数をクリア, しかし、そのように全体の古いデータが無効になります. これを行うには嫌がっています, 彼らはいずれかの回避を避ける場合.
-
oldVersionを使用して、彼の状態 < 2 編集テーブルを実行します. これは、段階的にデータベースを編集することができます. たとえば、次の時間DATA_VERSION = 3 あなたはif文を追加します(oldVersion < 3) と実装. それはあなたが相がデータベースを更新思い出すのに役立ち、また、アプリの更新間隔を可能にします (から例えば 1.0 ノットアップ 2.0 しかしアップ 3.0 右) 更新エラーはありません.
-
このフィールドには、デフォルトに自分自身をLAST_MODIFIED空の文字列です, レコードはヌルされていなかったヘルプ, エラー検索を回避.
関連検索方法を更新
このセクションでは, 我々は修正する必要があります 2 方法には、入れて、十分な情報を取り出すことができるように注意して、データベースからContentValuesにメモを取ります.
/**
* convert note to contentvalues
* don't put id of note because
* when insert id will auto create
* when update we don't update id
*/
private ContentValues noteToValues(Note note) {
ContentValues values = new ContentValues();
values.put(KEY_TITLE_NOTE, note.getTitle());
values.put(KEY_CONTENT_NOTE, note.getContent());
values.put(KEY_LAST_MODIFIED_NOTE, note.getLastModified());
return values;
}
/**
* convert cursor to note
*/
private Note cursorToNote(Cursor cursor) {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID_NOTE)))
.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE_NOTE)))
.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT_NOTE)))
.setLastModified(cursor.getString(cursor.getColumnIndex(KEY_LAST_MODIFIED_NOTE)));
return note;
}
ステップ 3: 更新新しいデータ作成
少し混乱見える聞くが、それは簡単です, クラスの正しい方法は、時間の更新ノートを保存するために保存されているNoteAcitivty.
/**
* get title and content and update last modified time of note, if they empty then finish
* if they not empty then check note is null?
* if note is null (create new note), we will create note and insert into database
* if note not null then we update note
* after save we finish activity
*/
private void save() {
String title = editTitle.getText().toString().trim();
String content = editContent.getText().toString().trim();
String notify = null;
if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
notify = "note empty, don't save!";
} else {
// get curren time for last modified
SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currenTime = formatTime.format(cal.getTime());
// new note
if (note == null) {
Note note = new Note();
note.setTitle(title).setContent(content).setLastModified(currenTime);
if (db.insertNote(note) > 0) {
notify = "add success!";
} else {
notify = "add fail!";
}
} else { // update note
note.setTitle(title).setContent(content).setLastModified(currenTime);
if (db.updateNote(note)) {
notify = "update success!";
} else {
notify = "update fail!";
}
}
}
Toast.makeText(context, notify, Toast.LENGTH_SHORT).show();
finish();
}
ステップ 4: 更新インタフェース
データベースファイルに編集を行った後, 我々はそれに応じて編集インターフェースを返します.
各項目について、よりを表示します 1 LAST_MODIFIED左学校は底にあります.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
card_view:cardCornerRadius="1dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/backgroud_item_note"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:text="@string/title_note"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:text="@string/content"
android:textSize="13sp" />
<TextView
android:id="@+id/tv_last_modified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:textSize="13sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
最後に、それに応じて改訂された退職アダプタ. 私達はちょうど修正する必要があります 2 クラスを配置 RecyclerViewHolder ItemNoteAdapterおよび方法で onBindViewHolder
/**
* ViewHolder for item view of list
*/
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements
OnClickListener {
public LinearLayout container;
public TextView tvTitle;
public TextView tvContent;
public TextView tvLastModified; //add
public RecyclerViewHolder(View itemView) {
super(itemView);
container = (LinearLayout) itemView.findViewById(R.id.item_container);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
tvContent = (TextView) itemView.findViewById(R.id.tv_content);
tvLastModified = (TextView) itemView.findViewById(R.id.tv_last_modified);
container.setOnClickListener(this);
}
// click item then display note
@Override
public void onClick(View v) {
MainActivity.showNote(context, list.get(getPosition()).getId());
}
}
/**
* set data for item
*/
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
Note note = list.get(position);
viewHolder.tvTitle.setText(note.getTitle());
viewHolder.tvContent.setText(note.getContent());
viewHolder.tvLastModified.setText(note.getLastModified());
}
ファスナーは、最も重要なポストが完了したされています. 次のセクションでは、私は、メモリカードにデータをバックアップする方法をご案内します.
チュートリアルで行われた投稿 データベースtrongのアンドロイド によって nguyenvanquan7826



あなた自身が尋ねました, 私はこれを行う理由は、その時はそれを共有します. それだけで “2017-03-31 00:56:07”. 営業時間は、 00
レビューの日付タイムコードフォーマットは、星の光を見ます.