public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize(getApplicationContext());
try{
// 'data/data'에 생성된 db파일 읽어오기
SQLiteDatabase db = openOrCreateDatabase("zz2.db", Context.MODE_PRIVATE, null);
// 쿼리로 db의 커서 획득
Cursor cur = db.rawQuery("SELECT * From db", null);
// 처음 레코드로 이동
cur.moveToFirst();
// 읽은값 출력
Log.i("move!!!",""+cur.getString(0));
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cur, null, null);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
//TextView tv = (TextView)findViewById(R.id.textView); //텍스트로하면 나옴!
//tv.setText(cur.getString(0));
}catch(Exception e){
Log.i("_)",""+e.toString());
}
}
public static final String PACKAGE_DIR = "/data/data/com.test.db/";
public static final String DATABASE_NAME = "zz.db";
public static final String COPY2DATABASE_NAME = "zz2.db";
public static void initialize(Context ctx) {
// check
File folder = new File(PACKAGE_DIR + "databases");
folder.mkdirs();
File outfile = new File(PACKAGE_DIR + "databases/" + COPY2DATABASE_NAME);
if (outfile.length() <= 0) {
AssetManager assetManager = ctx.getResources().getAssets();
try {
InputStream is = assetManager.open(DATABASE_NAME, AssetManager.ACCESS_BUFFER);
long filesize = is.available();
byte [] tempdata = new byte[(int)filesize];
is.read(tempdata);
is.close();
outfile.createNewFile();
FileOutputStream fo = new FileOutputStream(outfile);
fo.write(tempdata);
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
위에 소스중에 TextView 를 쓰면 텍스트뷰에 db에 목록이 잘뜹니다..
근데 ListView로 구성하고 싶은데 소스를
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cur, null, null);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
이런식으로 했는데 안뜹니다.. 어떤점이 잘못됬나요..?