public
class
SecondActivity
extends
Activity {
public
static
final
String TAG =
"SecondActivity"
;
DataListView listView;
IconTextListAdapter adapter;
InputMethodManager imm;
String strSearch2;
String strSearchQuery;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Intent intent = getIntent();
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
adapter =
new
IconTextListAdapter(
this
);
listView =
new
DataListView(
this
);
LinearLayout linLayout = (LinearLayout) findViewById(R.id.LayoutDicList);
linLayout.addView(listView);
strSearch2 =
"로"
;
strSearchQuery = strSearch2.concat(
"%"
);
DatabaseHelper.openDatabase(DatabaseHelper.wordDatabaseFile);
Cursor cursor = DatabaseHelper.queryMasterTable(strSearchQuery);
AddCursorData(cursor);
listView.setAdapter(adapter);
listView.setOnDataSelectionListener(
new
OnDataSelectionListener () {
public
void
onDataSelected(AdapterView parent, View v,
int
position,
long
id) {
IconTextItem selectItem = (IconTextItem)adapter.getItem(position);
Bundle bundle =
new
Bundle();
bundle.putString(
"data0"
, selectItem.getData(
0
));
bundle.putString(
"data1"
, selectItem.getData(
1
));
Intent intent =
new
Intent( getApplicationContext(), DetailActivity.
class
);
intent.putExtras(bundle);
startActivity ( intent );
}
});
}
protected
void
onDestroy() {
super
.onDestroy();
DatabaseHelper.closeDatabase();
}
public
void
AddCursorData ( Cursor outCursor ) {
int
recordCount = outCursor.getCount();
println(
"cursor count : "
+ recordCount +
"\n"
);
adapter.clear();
int
clasFromCol = outCursor.getColumnIndex(
"CLASFROM"
);
int
clasSoutCol = outCursor.getColumnIndex(
"CLASSOUT"
);
Resources res = getResources();
for
(
int
i =
0
; i < recordCount; i++) {
outCursor.moveToNext();
String clasFrom = outCursor.getString(clasFromCol);
String clasSout = outCursor.getString(clasSoutCol);
adapter.addItem(
new
IconTextItem(res.getDrawable(R.drawable.capsule1),clasSout,clasFrom));
}
outCursor.close();
}
public
void
println(String msg) {
Log.d(TAG, msg);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
int
id = item.getItemId();
if
(id == R.id.action_settings) {
return
true
;
}
return
super
.onOptionsItemSelected(item);
}
}
데이터베이스 헬퍼
public
class
DatabaseHelper {
public
static
final
String TAG =
"DatabaseHelper"
;
public
static
SQLiteDatabase db;
public
static
String wordDatabaseFile =
"/sdcard/wordinfo.db"
;
public
DatabaseHelper(){}
public
static
void
openDatabase(String databaseFile){
println(
"creating or opening database ["
+ wordDatabaseFile +
"]."
);
try
{
db = SQLiteDatabase.openDatabase(
databaseFile,
null
,SQLiteDatabase.OPEN_READWRITE);
}
catch
(SQLiteException ex) {
}
}
public
static
void
closeDatabase(){
try
{
db.close();
}
catch
(Exception ext) {
ext.printStackTrace();
println(
"Exception in closing database : "
+ ext.toString());
}
}
private
static
void
createMasterTable() {
try
{
db.execSQL(
"drop table if exists MASTER"
);
db.execSQL(
"create table MASTER("
+
" WORDFROM text, "
+
" WORDNAME text, "
+
" CLASFROM text, "
+
" CLASSOUT text)"
);
}
catch
(SQLiteException ex) {
}
}
public
static
boolean
insertMasterData(String aLine) {
String[] tokens = aLine.split(
"\\|"
);
if
(tokens !=
null
&& tokens.length >
3
) {
println(
"length of tokens : "
+ tokens.length);
db.execSQL(
"insert into MASTER(WORDFROM, WORDNAME, CLASFROM, CLASSOUT) values ("
+
"'"
+ tokens[
0
] +
"',"
+
"'"
+ tokens[
1
] +
"',"
+
"'"
+ tokens[
2
] +
"',"
+
"'"
+ tokens[
3
] +
"')"
);
return
true
;
}
else
{
println(
"the input line is invalid."
);
}
return
false
;
}
public
static
Cursor queryMasterTable(String strSearchWord) {
String aSQL =
"select WORDFROM, WORDNAME, CLASFROM, CLASSOUT "
+
" from MASTER"
+
" where CLASSOUT like ?"
;
String[] args = {strSearchWord};
Cursor outCursor = db.rawQuery(aSQL, args);
return
(outCursor);
}
public
static
Cursor queryDetailsTable(String strDrugCode) {
String aSQL =
"select WORDNAME, CLASSNAME, DETAILS "
+
" from DETAILS"
+
" where WORDNAME = ?"
;
String[] args = {strDrugCode};
Cursor outCursor = db.rawQuery(aSQL, args);
return
(outCursor);
}
public
static
void
println(String msg) {
Log.d(TAG, msg);
}
}