public
class
CalDicActivity
extends
Activity {
private
static
String DB_PATH =
"/sdcard/"
;
private
static
String DB_NAME =
"testjye.sqlite"
;
private
int
listcount =
0
;
private
String[] foodsList =
null
;
private
String[] calList =
null
;
private
String[] gramList =
null
;
ArrayAdapter<String> arrayAdapter;
private
ArrayList<String> listString;
private
ArrayList<String> listString2;
private
EditText input;
private
String Error =
null
;
private
String stredit;
....
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.caldic);
input=(EditText)findViewById(R.id.input);
stredit = input.getText().toString();
try
{
createDataBase();
}
catch
(IOException ioe) {
Toast.makeText(
this
,
"DB 파일을 생성할 수 없습니다."
, Toast.LENGTH_LONG).show();
}
listcount =
0
;
try
{
Cursor cursor;
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME,
null
,
1
);
String[] FROM = {
"*"
};
cursor = db.query(
"foodcala"
, FROM,
null
,
null
,
null
,
null
,
null
);
startManagingCursor(cursor);
foodsList =
new
String[cursor.getCount()];
calList =
new
String[cursor.getCount()];
gramList =
new
String[cursor.getCount()];
while
(cursor.moveToNext())
{
String foods = cursor.getString(
1
);
String cal = cursor.getString(
2
);
String gram = cursor.getString(
3
);
foodsList[listcount] = foods;
calList[listcount] = cal;
gramList[listcount] = gram;
listcount++;
}
if
(db !=
null
)
db.close();
}
catch
(Exception e) {
Toast.makeText(
this
,
"ERROR IN CODE:"
+e.toString(), Toast.LENGTH_LONG).show();
}
if
(listcount >
0
)
{
ArrayList<String> listString =
new
ArrayList<String>();
ArrayList<String> listString2 =
new
ArrayList<String>();
for
(
int
i =
0
; i < foodsList.length; i++)
{
listString.add(foodsList[i]+
" "
+calList[i]+
"kcal"
);
if
(foodsList[i]==stredit){
listString2.add(foodsList[i] + calList[i]);
}
}
ListView listView = (ListView)findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new
ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, listString);
listView.setAdapter(arrayAdapter);
}
input.addTextChangedListener(
new
TextWatcher(){
public
void
afterTextChanged(Editable s) {
}
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after) {
}
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {
if
(s.length()==
0
)
{
ListView listView = (ListView)findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new
ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, listString);
listView.setAdapter(arrayAdapter);
}
else
{
ListView listView = (ListView)findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new
ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1, listString2);
listView.setAdapter(arrayAdapter);
}
}
});
if
(Error !=
null
) {
Toast.makeText(CalDicActivity.
this
, Error, Toast.LENGTH_LONG).show();
}
else
{
}
}
}