Android | データベースから取得した情報をListViewで一覧表示する

2013-06-12
assetsにデータベースを入れる。
srcフォルダにMySQLiteOpenHelper.javaを作る

MySQLiteOpenHelper.javaのソース

[java]
package com.sample.sample;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class MySQLiteOpenHelper extends SQLiteOpenHelper
{
private static String TAG = "myTag";

private static String DB_PATH = "/data/data/com.sample.sample/databases/";

private static String DB_NAME = "sample.db";

private Context context;
SQLiteDatabase db;

public MySQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
Log.d(TAG, "MySQLOpenHandler onCreate()");
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
Log.d(TAG, "MySQLOpenHelper onUpgrade()");
}
@Override
public synchronized void close()
{
super.close();
Log.d(TAG, "mySQLOpenHelper clsoe()");
if (db != null)
db.close();
}
public SQLiteDatabase openDatabase()
{
String myPath = DB_PATH + DB_NAME;
File myFile = new File(myPath);
if (!myFile.exists())
copyDatabase();
try
{
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e)
{
Log.e(TAG,"SQLE:"+e.toString());
}
return db;
}
private void copyDatabase()
{
this.getReadableDatabase();
String myPath =DB_PATH + DB_NAME;
try
{
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(myPath);

byte[] buffer = new byte[1024];
int size = 0;
while((size = is.read(buffer))>0)
{
os.write(buffer,0,size);
}
os.flush();
os.close();
is.close();
}
catch (FileNotFoundException e)
{
Log.d(TAG, "FNFE:"+e.toString());
}
catch (IOException e)
{
Log.d(TAG, "IOE:"+e.toString());
}
}
}
[/java]

MainActivity.javaのソース

[java]
package com.sample.sample;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity{

private static final String TAG = "myTag";
public static final String DATA_TAG = "myData";

protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_main);

MySQLiteOpenHelper msoh = new MySQLiteOpenHelper(this);
SQLiteDatabase db = msoh.openDatabase();

if (db != null)
{
try
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
String sql = "SELECT * "+"FROM SAmpleTable ORDER BY SampleID ASC";
Cursor csr = db.rawQuery(sql, null);
csr.moveToFirst();
for (int i = 0; i < csr.getCount(); i++)
{
String str = csr.getString(5);
adapter.add(str);
csr.moveToNext();

}
csr.close();
db.close();
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
catch(SQLException e)
{
Log.e(TAG, "SQLExcepption:"+e.toString());
}
}
}
}
[/java]

xmlのソース

[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_horizontal"
android:orientation="vertical" >

<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>
[/xml]

で良いはず・・・

Copyright(c) 2015 AndByYOU INC. All Rights Reserved.