Created
October 17, 2013 06:37
-
-
Save shikajiro/7020044 to your computer and use it in GitHub Desktop.
DBの作成、挿入、カーソル取得を使ったListVIewの使い方。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.dhw; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteDatabase.CursorFactory; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class DBHelper extends SQLiteOpenHelper { | |
public DBHelper(Context context, String name, CursorFactory factory, int version) { | |
super(context, name, factory, version); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL("CREATE TABLE address (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, gender TEXT);"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { | |
// TODO Auto-generated method stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.dhw; | |
import android.app.Activity; | |
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.ListView; | |
import android.widget.SimpleCursorAdapter; | |
public class MainActivity extends Activity { | |
private DBHelper dbHelper; | |
private SimpleCursorAdapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ListView listView = (ListView) findViewById(R.id.listView); | |
//DBHelpderを作成する。この時にDBが作成される。 | |
dbHelper = new DBHelper(this, "app", null, 1); | |
//DBを読み込み可能状態で開く。 | |
//※getWritableDatabase(書き込み可能状態でも読み込みはできる) | |
SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
//DBへクエリーを発行し、カーソルを取得する。 | |
String[] columns = new String[]{"_id", "name", "gender"}; | |
Cursor cursor = db.query("address", columns, null, null, null, null, null); | |
//取得したカーソルをカーソル用のアダプターに設定する。 | |
String[] headers = new String[]{"name", "gender"}; | |
int[] layouts = new int[]{android.R.id.text1, android.R.id.text2}; | |
adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, cursor, headers, layouts); | |
listView.setAdapter(adapter); | |
//dbHelper と adapterはこれ以降も使う変数なので、メンバ変数にしておく。 | |
} | |
/* | |
* add ボタンをメニューに追加する。 | |
*/ | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
menu.add(0, 1, 0, "add"); | |
return super.onCreateOptionsMenu(menu); | |
} | |
/* | |
* add ボタンを押した時の処理を定義する。 | |
* addボタンを押すと、特定のデータをDBのテーブルに挿入(Insert)する。 | |
*/ | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if(item.getItemId() == 1){ | |
//DBに書き込みするので、getWritableDatabaseでdbと接続する。 | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
//挿入したいデータを作成する。 | |
ContentValues values = new ContentValues(); | |
values.put("name", "shika"); | |
values.put("gender", "mens"); | |
//挿入実行 | |
db.insert("address", null, values); | |
//挿入したことで、データベースの中身に変更があった。 | |
//そのため、再度カーソルを取得し、カーソル変更と、データ変更があったことをアダプターに通知する。 | |
String[] columns = new String[]{"_id", "name", "gender"}; | |
Cursor cursor = db.query("address", columns, null, null, null, null, null); | |
adapter.changeCursor(cursor); | |
adapter.notifyDataSetChanged(); | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment