Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

[DB연동] 본문

안드로이드/DB

[DB연동]

설.현.아빠 2011. 2. 11. 09:42




 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- main.xml -->


<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>

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

 

 

// Main.java

 

package com.myandroid.db;

 

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

 

public class Main extends Activity {
    /** 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;
  }
 };
}

 

 

// TodoList.java

 

package com.myandroid.db;

 

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.BaseAdapter;
import android.widget.SimpleAdapter;

public class TodoList {
 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;
}

 


Comments