설.현.아빠
[DB연동] 본문
<?xml version="1.0" encoding="utf-8"?> <!-- main.xml --> <ListView // Main.java package com.myandroid.db; import android.app.Activity; public class Main extends Activity { // TodoList.java package com.myandroid.db; import java.util.ArrayList; import android.app.Activity; public class TodoList {
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:text=""
android:id="@+id/edTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
android:id="@+id/ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_TodoList= new TodoList(this);
_edTitle = (EditText)findViewById(R.id.edTitle);
_edTitle.setOnKeyListener(on_keyEvent);
_ListView = (ListView)findViewById(R.id.ListView);
_ListView.setAdapter(_TodoList.Adapter);
}
private TodoList_TodoList= null;
private EditText _edTitle = null;
private ListView _ListView = null;
private View.OnKeyListener on_keyEvent = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN) {
if(keyCode==KeyEvent.KEYCODE_ENTER) {
_TodoList.Append(_edTitle.getText().toString());
_edTitle.setText("");
return true;
}
}
return false;
}
};
}
import java.util.HashMap;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.BaseAdapter;
import android.widget.SimpleAdapter;
public TodoList(Activity AActivity) {
super();
_Activity = AActivity;
_DB = _Activity.openOrCreateDatabase("Ryu2", _Activity.MODE_PRIVATE, null);
_DB.execSQL("Create Table IF NOT EXISTS TodoList (SN integer Primary Key AutoIncrement,Title varchar)");
Adapter = new SimpleAdapter(
_Activity,
_DataSource,
android.R.layout.simple_list_item_2, //두개의 TextView를 가진 Layout
new String[] {"SN","Title"}, //필드명
new int[] {android.R.id.text1, android.R.id.text2} // ID
);
Refresh();
}
// 자동 순번 입력된걸 가져오는 함수
public int getLastRowID() {
Cursor _Cursor = _DB.rawQuery("Select last_insert_rowid() from TodoList",null);
if(_Cursor.moveToFirst()) {
return _Cursor.getInt(0);
}
else {
return -1;
}
}
public void Append(String ATitle) {
_DB.execSQL("Insert into TodoList (Title) values ('"+ATitle+"')");
HashMap<String,String> _Item = new HashMap<String,String>();
_Item.put("SN", Integer.toString(getLastRowID()));
_Item.put("Title", ATitle);
_DataSource.add(_Item);
Adapter.notifyDataSetChanged();
}
public void Refresh() {
Cursor _Cursor = _DB.rawQuery("Select * from TodoList", null);
if(_Cursor.moveToFirst()) {
do{
HashMap<String,String> _Item = new HashMap<String,String>();
_Item.put("SN", _Cursor.getString(0).toString());
_Item.put("Title", _Cursor.getString(1).toString());
_DataSource.add(_Item);
}while(_Cursor.moveToNext());
}
Adapter.notifyDataSetChanged(); //이 함수가 호출되었을 때 UI에 적용됨.
}
private SQLiteDatabase _DB = null;
public BaseAdapter Adapter = null;
private ArrayList<HashMap<String,String>> _DataSource = new ArrayList<HashMap<String,String>>();
private Activity _Activity = null;
}
'안드로이드 > DB' 카테고리의 다른 글
DB의 각종 Method 설명(WHERE절 텍스트 비교법) (0) | 2011.05.20 |
---|---|
[DBMS] 공개 database 서버 (1) | 2011.05.12 |
데이터베이스 이용하기 (커니의 안드로이드 이야기) (0) | 2011.03.17 |
기본 DB schema (3) | 2011.02.11 |