https://arabiannight.tistory.com/entry/368?category=476074
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // DataBase Table public final class DataBases { public static final class CreateDB implements BaseColumns{ public static final String NAME = "name" ; public static final String CONTACT = "contact" ; public static final String EMAIL = "email" ; public static final String _TABLENAME = "address" ; public static final String _CREATE = "create table " +_TABLENAME+ "(" +_ID+ " integer primary key autoincrement, " +NAME+ " text not null , " +CONTACT+ " text not null , " +EMAIL+ " text not null );" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class DbOpenHelper { private static final String DATABASE_NAME = "addressbook.db" ; private static final int DATABASE_VERSION = 1 ; public static SQLiteDatabase mDB; private DatabaseHelper mDBHelper; private Context mCtx; private class DatabaseHelper extends SQLiteOpenHelper{ // 생성자 public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super (context, name, factory, version); } // 최초 DB를 만들때 한번만 호출된다. @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DataBases.CreateDB._CREATE); } // 버전이 업데이트 되었을 경우 DB를 다시 만들어 준다. @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL( "DROP TABLE IF EXISTS " +DataBases.CreateDB._TABLENAME); onCreate(db); } } public DbOpenHelper(Context context){ this .mCtx = context; } public DbOpenHelper open() throws SQLException{ mDBHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null , DATABASE_VERSION); mDB = mDBHelper.getWritableDatabase(); return this ; } public void close(){ mDB.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | public class TestDataBaseActivity extends Activity { private static final String TAG = "TestDataBaseActivity" ; private DbOpenHelper mDbOpenHelper; private Cursor mCursor; private InfoClass mInfoClass; private ArrayList<infoclass> mInfoArray; private CustomAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); setLayout(); // DB Create and Open mDbOpenHelper = new DbOpenHelper( this ); mDbOpenHelper.open(); mDbOpenHelper.insertColumn( "김태희" , "01000001111" , "angel@google.com" ); mDbOpenHelper.insertColumn( "송혜교" , "01333331111" , "asdffff@emdo.com" ); mDbOpenHelper.insertColumn( "낸시랭" , "01234001111" , "yaya@hhh.com" ); mDbOpenHelper.insertColumn( "제시카" , "01600001111" , "tree777@atat.com" ); mDbOpenHelper.insertColumn( "성유리" , "01700001111" , "tiger@tttt.com" ); mDbOpenHelper.insertColumn( "김태우" , "01800001111" , "gril@zzz.com" ); // startManagingCursor(mCursor); mInfoArray = new ArrayList<infoclass>(); doWhileCursorToArray(); for (InfoClass i : mInfoArray){ DLog.d(TAG, "ID = " + i._id); DLog.d(TAG, "name = " + i.name); DLog.d(TAG, "contact = " + i.contact); DLog.d(TAG, "email = " + i.email); } mAdapter = new CustomAdapter( this , mInfoArray); mListView.setAdapter(mAdapter); mListView.setOnItemLongClickListener(longClickListener); } @Override protected void onDestroy() { mDbOpenHelper.close(); super .onDestroy(); } /** * ListView의 Item을 롱클릭 할때 호출 ( 선택한 아이템의 DB 컬럼과 Data를 삭제 한다. ) */ private OnItemLongClickListener longClickListener = new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<!--?--> arg0, View arg1, int position, long arg3) { DLog.e(TAG, "position = " + position); boolean result = mDbOpenHelper.deleteColumn(position + 1 ); DLog.e(TAG, "result = " + result); if (result){ mInfoArray.remove(position); mAdapter.setArrayList(mInfoArray); mAdapter.notifyDataSetChanged(); } else { Toast.makeText(getApplicationContext(), "INDEX를 확인해 주세요." , Toast.LENGTH_LONG).show(); } return false ; } }; /** * DB에서 받아온 값을 ArrayList에 Add */ private void doWhileCursorToArray(){ mCursor = null ; mCursor = mDbOpenHelper.getAllColumns(); DLog.e(TAG, "COUNT = " + mCursor.getCount()); while (mCursor.moveToNext()) { mInfoClass = new InfoClass( mCursor.getInt(mCursor.getColumnIndex( "_id" )), mCursor.getString(mCursor.getColumnIndex( "name" )), mCursor.getString(mCursor.getColumnIndex( "contact" )), mCursor.getString(mCursor.getColumnIndex( "email" )) ); mInfoArray.add(mInfoClass); } mCursor.close(); } /** * OnClick Button * @param v */ public void onClick(View v){ switch (v.getId()) { case R.id.btn_add: mDbOpenHelper.insertColumn ( mEditTexts[Constants.NAME].getText().toString().trim(), mEditTexts[Constants.CONTACT].getText().toString().trim(), mEditTexts[Constants.EMAIL].getText().toString().trim() ); mInfoArray.clear(); doWhileCursorToArray(); mAdapter.setArrayList(mInfoArray); mAdapter.notifyDataSetChanged(); mCursor.close(); break ; default : break ; } } /* * Layout */ private EditText[] mEditTexts; private ListView mListView; private void setLayout(){ mEditTexts = new EditText[]{ (EditText)findViewById(R.id.et_name), (EditText)findViewById(R.id.et_contact), (EditText)findViewById(R.id.et_email) }; mListView = (ListView) findViewById(R.id.lv_list); } } </infoclass></infoclass> |
출처: https://arabiannight.tistory.com/68 [아라비안나이트]