안녕하세요. 간단한 DB소스를 이용하여 Database에서 다수의 Column을 생성하려고 합니다.
나름대로 머리를 쥐어짜가면서 이책 저책 서핑까지해보아도 잘 모르겠어서요.
소스첨부합니다.
정말 도저히 뭐가 문제인지 모르겠씁니다
흐엉엉엉
제발..부탁드립니다..ㅠ.ㅠ
p.s 생각해보면. ListView에 컬럼이 3개라 할지라도 입력받는 EditText에서 입력할수있는 공간은 하나이기 떄문에
그런거같기도한데, 해결방법이 생각나질않습니다..아니다.. 실력이없어서 그런걸지도..ㅠ.ㅠ
DBAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class DBAdapter {
private final Context context;
static final String DB="memodb.db"; //database 이름.
static final String TABLE = "memos"; //table 이름.
static final String COL1="memo";
static final String COL2="memo2";
static final String COL3="memo3";
static final int VERSION=1;
static final String CREATE = "create table " + TABLE +
" (_id integer primary key autoincrement, " +
COL1 + COL2 + COL3 + " text not null );";
static final String DROP = "drop table ";
private SQLiteDatabase db;
private MemoOpenHelper dbHelper;
public DBAdapter(Context ctx) {
context = ctx;
}
private static class MemoOpenHelper extends SQLiteOpenHelper {
public MemoOpenHelper(Context c) {
super(c, DB, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create TABLE " + DB + "(" +
COL1 + " TEXT PRIMARY KEY," +
COL2 + " TEXT," +
COL3 + " TEXT;");
db.execSQL(CREATE);
}
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
Log.w("debug", "Version mismatch :" + oldVersion +
" to " + newVersion );
db.execSQL(DROP + TABLE +";");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
dbHelper = new MemoOpenHelper(context);
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public Cursor fetchAllMemo()
return db.query(TABLE, new String[] {BaseColumns._ID, COL1,COL2,COL3},
null, null, null, null, null, "_id DESC");
public String addMemo(String memo, String memo2, String memo3) {
ContentValues values = new ContentValues();
values.put(COL1, memo);
values.put(COL2, memo2);
values.put(COL3, memo3);
long id = db.insert(TABLE, null, values);
if (id < 0)
return "";
return Long.toString(id);
}
public void deleteMemo(String id) {
db.delete(TABLE, "_id = ?", new String[] { id });
}
public void updateMemo(String id, String memo,String memo2,String memo3) {
ContentValues values = new ContentValues();
values.put(COL1, memo);
values.put(COL2, memo2);
values.put(COL3, memo3);
db.update(
TABLE, values, "_id = ?", new String[] { id });
}
}
DBtestActivity.java
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class DBtestActivity extends ListActivity {
static final String COL1="memo"; String COL2="memo2"; String COL3="memo3";
private SimpleCursorAdapter adapter;
private TextView textId;
private EditText editMemo;
public DBAdapter db;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DBAdapter(this);
db.open();
textId = (TextView)findViewById(R.id.textId);
editMemo = (EditText)findViewById(R.id.editMemo);
((Button)findViewById(R.id.add)).setOnClickListener(listener);
((Button)findViewById(R.id.delete)).setOnClickListener(listener);
((Button)findViewById(R.id.update)).setOnClickListener(listener);
setEnabled(false);
}
@Override
protected void onResume() {
super.onResume();
Cursor c = db.fetchAllMemo();
startManagingCursor(c);
String[] from = new String[] { BaseColumns._ID, COL1,COL2,COL3 };
int[] to = new int[] { R.id.id, R.id.memo };
adapter = new SimpleCursorAdapter(
this, R.layout.row, c, from, to);
setListAdapter(adapter); }
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onListItemClick(
ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView t = (TextView)v.findViewById(R.id.memo);
TextView t2 = (TextView)v.findViewById(R.id.memo2);//
TextView t3 = (TextView)v.findViewById(R.id.memo3);//
editMemo.setText(t.getText());
editMemo.setText(t2.getText());
editMemo.setText(t3.getText());
textId.setText(Long.toString(id));
setEnabled(true);//(2)
}
private void setEnabled(boolean enabled) {
((Button)findViewById(R.id.delete)).setEnabled(enabled);
((Button)findViewById(R.id.update)).setEnabled(enabled);
}
Button.OnClickListener listener = new View.OnClickListener() {
public void onClick(View view) {
String id = textId.getText().toString();
String str = editMemo.getText().toString();
switch(view.getId()) {//(4)
case R.id.delete:
db.deleteMemo(id);
break;
case R.id.update:
if (!str.isEmpty())
db.updateMemo(id, str, str, str);
else
db.deleteMemo(id);
break;
case R.id.add:
if (!str.isEmpty())
db.addMemo(str, str, str);
break;
default:
Toast.makeText(getBaseContext(),
"Error!", Toast.LENGTH_LONG).show();//(4)
}
textId.setText("");
editMemo.setText("");
setEnabled(false);
Cursor c = db.fetchAllMemo();
startManagingCursor(c);
adapter.changeCursor(c);
}
};
Res / layout
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<TextView
android:id="@+id/textId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<EditText
android:id="@+id/editMemo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<Button
android:id="@+id/add"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="Add"
/>
<Button
android:id="@+id/update"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:enabled="false"
android:text="Update"
/>
<Button
android:id="@+id/delete"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="Delete"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:choiceMode="singleChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No memo found!"
/>
</LinearLayout>
Row.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</LinearLayout>
<TextView
android:id="@+id/id"
android:layout_width="50sp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/memo"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:padding="5px" />
<TextView
android:id="@+id/memo2"
android:layout_width="414dp"
android:layout_height="wrap_content"
android:padding="5px" />
<TextView
android:id="@+id/memo3"
android:layout_width="604dp"
android:layout_height="wrap_content"
android:padding="5px" />
</LinearLayout>




뭐가 문젠지 로그도 없고..소스만 투척하시면 누가 답변을 달아줄지..-_-;