dotinstallのコード
MainActivity.java
package com.example.shohei.myapplication; import android.app.Activity; import android.app.LoaderManager; import android.content.ContentValues; import android.content.CursorLoader; import android.content.Loader; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{ private SimpleCursorAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] from = { MyAppContract.Users.COLUMN_NAME, MyAppContract.Users.COLUMN_SCORE }; int[] to = { android.R.id.text1, android.R.id.text2 }; adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, null, from, to, 0 ); ListView myListView = (ListView) findViewById(R.id.myListView); myListView.setAdapter(adapter); getLoaderManager().initLoader(0,null,this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] projection = { MyAppContract.Users.COLUMN_ID, MyAppContract.Users.COLUMN_NAME, MyAppContract.Users.COLUMN_SCORE }; return new CursorLoader( this, MyContentProvider.CONTENT_URI, projection, null, null, null ); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } }
MyDbHelper.java
package com.example.shohei.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by shohei on 4/13/15. */ public class MyDbHelper extends SQLiteOpenHelper { private final static String DB_NAME = "myapp.db"; private final static int DB_VERSION = 2; MyDbHelper(Context context){ super(context,DB_NAME,null,DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { //create and init table db.execSQL(MyAppContract.Users.CREATE_TABLE); db.execSQL(MyAppContract.Users.INIT_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //drop table db.execSQL(MyAppContract.Users.DROP_TABLE); onCreate(db); } }
MyAppContract.java
package com.example.shohei.myapplication; import android.provider.BaseColumns; /** * Created by shohei on 4/13/15. */ public final class MyAppContract { public MyAppContract(){} public static abstract class Users implements BaseColumns{ public static final String TABLE_NAME = "users"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_NAME = "name"; public static final String COLUMN_SCORE = "score"; public static final String CREATE_TABLE = "create table "+TABLE_NAME+"( "+ COLUMN_ID+" integer primary key autoincrement, "+ COLUMN_NAME+" text, "+ COLUMN_SCORE+" integer)"; public static final String INIT_TABLE = "insert into users (name,score) values "+ "('taguchi',20),('fkoji',30),('dotinstall',49)"; public static final String DROP_TABLE = "drop table if exists "+TABLE_NAME; } }
MyContentProvider.java
package com.example.shohei.myapplication; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import java.util.IllegalFormatCodePointException; /** * Created by shohei on 4/14/15. */ public class MyContentProvider extends ContentProvider{ private static final String AUTHORITY = "com.example.shohei.myapplication.mycontentprovider"; public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/"+MyAppContract.Users.TABLE_NAME); private MyDbHelper myDbHelper; private static final int USERS = 1; private static final int USER_ITEM = 2; private static final UriMatcher uriMatcher; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY,MyAppContract.Users.TABLE_NAME,USERS); uriMatcher.addURI(AUTHORITY,MyAppContract.Users.TABLE_NAME+"/#",USER_ITEM); } @Override public boolean onCreate() { myDbHelper = new MyDbHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if(uriMatcher.match(uri) != USERS){ throw new IllegalArgumentException("Unknown URI: "+uri); } SQLiteDatabase db = myDbHelper.getReadableDatabase(); Cursor cursor = db.query( MyAppContract.Users.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder ); return cursor; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shohei.myapplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"> <application> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:authorities="com.example.shohei.myapplication.mycontentprovider" android:name="com.example.shohei.myapplication.MyContentProvider" android:exported="false"> </provider> </application> </manifest>
layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myListView" /> </LinearLayout>