DBManager.java
package com.andro.dbproj;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBManager extends SQLiteOpenHelper{
public DBManager(Context context) {
super(context, "myDB", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("create table members (name text, sex text, sms text, interest text);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
그리고 QueryActivity.java
package com.andro.dbproj;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class QueryActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv_list = (TextView) findViewById (R.id.list);
int i=0;
try{
DBManager dbmgr = new DBManager(this);
SQLiteDatabase sdb = dbmgr.getReadableDatabase();
Cursor cursor = sdb.rawQuery("select name, sex, sms, interest from members", null);
while(cursor.moveToNext()){
String name = cursor.getString(0);
String sex = cursor.getString(1);
String sms = cursor.getString(2);
String interest = cursor.getString(3);
tv_list.append(name + "\n");
tv_list.append(sex + "\n");
tv_list.append(sms + "\n");
tv_list.append(interest + "\n");
i++;
}
cursor.close();
dbmgr.close();
} catch (SQLiteException e) {
tv_list.append("DB 에러입니다");
}
if(i == 0)
tv_list.append("저장된 정보가 없습니다!\n");
Button btn = (Button)findViewById(R.id.button_join_form);
btn.setOnClickListener(this);
}
public void onClick (View v){
Intent it = new Intent(this, JoinActivity.class);
startActivity(it);
finish();
}
}
마지막 JoinActivity.java
package com.andro.dbproj;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class JoinActivity extends Activity implements OnClickListener {
private DBManager dbmgr;
Spinner spinner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.join_form);
spinner = (Spinner) findViewById (R.id.spinner_interest);
ArrayAdapter<CharSequence> adapter=
ArrayAdapter.createFromResource(this, R.array.interst_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button btn = (Button)findViewById(R.id.button_send);
btn.setOnClickListener(this);
}
public void onClick(View v) {
EditText et_name = (EditText)findViewById(R.id.edit_name);
String str_name = et_name.getText().toString();
RadioGroup rg_sex = (RadioGroup)findViewById(R.id.radiogroup_sex);
String str_sex = "";
if(rg_sex.getCheckedRadioButtonId() == R.id.radio_male) {
str_sex = "남";
}
if(rg_sex.getCheckedRadioButtonId() == R.id.radio_female) {
str_sex = "남";
}
CheckBox chk_sms = (CheckBox)findViewById(R.id.checkbox_sms);
String str_sms = "";
if(chk_sms.isChecked()){
str_sms = (String)chk_sms.getText();
}
String str_interest = spinner.getSelectedItem().toString();
try {
dbmgr = new DBManager(this);
SQLiteDatabase sdb;
sdb = dbmgr.getWritableDatabase();
sdb.execSQL("insert into members values(" + str_name + ", " + str_sex + ", " + str_sms + ", " +str_interest + ");");
dbmgr.close();
}catch (SQLiteException e) {
}
Intent it = new Intent(this, QueryActivity.class);
startActivity(it);
finish();
}
}
이렇게 다 짜고 xml은 뭐 이상없고 안드로이드 manifest에도 실행할수 있게 엑티비티 다 넣엇는데
자꾸 쏘리가 떠요 ㅠㅠ 어떻게 해야하나요;; 책이 잘 못된것 같은데 무슨 문제인자,,,