{
ListDB.deleteAll();
}
지금 리스트뷰로 데이터를 출력하고있는데요..
해당 리스트를 클릭하면 이벤트까지 발생하는 것까지 해보았구요.. 이제 리스트 한개만을 삭제를 해야하는데..
고수님들 답변좀 부탁드립니다..
ListDB.deleteNode(rowId); 이걸로 해야하나요..? 여기에 어뎁터 포지션을 주면 되나요..? 잘안되서 질문드립니다.
제가 초보라서 잘 모르겠는데요..
ListDB가 아래의 클랙스 쪽으로 연결이 되더군요..SQL인가..
public boolean deleteNode(int rowId){
return mDB.delete(DB_TABLE, "=" +rowId , null)>0;
}
public DB_source deleteAll(){
Log.v("log_del","DB_del");
mDB.execSQL("DELETE FROM protect_v1");
return this;
}
아래처럼 쓰고 지우고 해도 안되요..
getDB_write();
DB_del();
closeDB();
arGeneral.remove(ListDB);
arGeneral.clear();
Adapter.notifyDataSetChanged();
저도 다른 소스를 가져온 거라서요.. 쫌 어렵네요..도와주세요..
어떤 경우에는 같다고 할수도 있습니다.
결국 DB 테이블에 저장된 키가 뭐냐.... 라는 건데요...
getItemId(position) 이런식으로 하는게 맞을지도 모르겠네요...
문제는 getItemId가 제대로 되어있는가가 좀 애매하고...
테이블 구조를 보고 판단해야할거 같군요... 그리고 저 LIstDB라는 클래스하고...
이게 ListDB 클래스입니다.
public class List_DB extends ListActivity {
ArrayList<String> arGeneral;
public DB_source ListDB;
private Cursor LCursor;
private Toast mToast;
private ArrayAdapter<String> Adapter;
protected static final int DIALOG_YES_NO_MESSAGE = 0;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getDB_read();
DB_look();
Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arGeneral);
setListAdapter(Adapter);
closeDB();
}
public void onListItemClick(ListView list,View view,int position,long id){
String mes;
//mes="Select Item="+arGeneral.get(position);
//Toast.makeText(List_DB.this, mes, Toast.LENGTH_SHORT).show();
}
void getDB_write()
{
ListDB = new DB_source(this);
ListDB.open("getwrite");
}
void getDB_read(){
ListDB=new DB_source(this);
ListDB.open("getread");
}
void closeDB()
{
if(ListDB != null)
ListDB.close();
ListDB = null;
}
void DB_input(String location)
{
ListDB.creatNode(location);
}
void DB_look()
{
arGeneral=new ArrayList<String>();
Cursor cursor;
cursor=ListDB.fetchAllNotes();
String Result="";
cursor.moveToFirst();
if(cursor.isFirst())
{
Log.v("cursor loc","it's first of Cursor");
String location=cursor.getString(0);
arGeneral.add(location);
}
while(cursor.moveToNext()){
String location=cursor.getString(0);
arGeneral.add(location);
}
if(cursor.getCount()!=0){
//mToast.makeText(List_DB.this, "안녕",Toast.LENGTH_SHORT);
}else{
//mToast.makeText(List_DB.this, "안녕",Toast.LENGTH_SHORT);
}
cursor.close();
}
void DB_del_All()
{
ListDB.deleteAll();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item=menu.add(0,1,0,"DB_del");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
//menu.add(0,1,0,"吏쒖옣").setIcon(R.drawable.icon).setAlphabeticShortcut('a');
menu.add(0,2,0,"萸먮꽔?꾧퉴?").setIcon(R.drawable.icon);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
getDB_write();
DB_del_All();
closeDB();
arGeneral.remove(arGeneral);
arGeneral.clear();
Adapter.notifyDataSetChanged();
return true;
}
return false;
}
}
그리고 이건 DB_soure 입니다.
public class DB_source {
private static final String KEY_LOCATION ="db_location";
private static final String DB_NAME="child_protected.db";
private static final String DB_TABLE="protect_v1";
private static final int DB_VERSION=1;
private final Context mCtx;
private DBHelper mDBHelper;
private SQLiteDatabase mDB;
private static final String DB_creat_table =
"CREATE TABLE protect_v1(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+"db_location TEXT NOT NULL"
+");";
private static final String ON_UPGRADE= "DROP TABLE IF EXISTS protect_v1";
private static class DBHelper extends SQLiteOpenHelper{
DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v("DB.onc","DB_start");
db.execSQL(DB_creat_table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(ON_UPGRADE);
onCreate(db);
}
}
public DB_source(Context ctx){
this.mCtx=ctx;
}
public DB_source open(String select) throws SQLException{
mDBHelper = new DBHelper(mCtx);
if(select =="getwrite"){
mDB=mDBHelper.getWritableDatabase();
Log.v("log_open","DB_wirte_open");
}
else if(select=="getread"){
mDB=mDBHelper.getWritableDatabase();
Log.v("log_open","DB_read_open");
}
else{
Log.v("log_open_fall","DB_read_open_fall");
}
return this;
}
public void close(){
mDBHelper.close();
Log.v("log_close","DB_close");
}
public long creatNode(String location){
ContentValues row=new ContentValues();
Log.v("log_input","DB_input");
row.put(KEY_LOCATION, location);
return mDB.insert(DB_TABLE, null, row);
}
public boolean deleteNode(int rowId){
return mDB.delete(DB_TABLE, "=" +rowId , null)>0;
}
public DB_source deleteAll(){
Log.v("log_del","DB_del");
mDB.execSQL("DELETE FROM protect_v1");
return this;
}
public Cursor fetchAllNotes(){
return mDB.query(DB_TABLE, new String[]{KEY_LOCATION}
, null, null, null, null, null);
}
public Cursor fetchNote(int rowId) throws SQLException{
Cursor mCursor=
mDB.query(false, DB_TABLE, new String[]{KEY_LOCATION},
"="+rowId, null, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateNote(int rowId,int day,int time,String location,String gps_point){
ContentValues args = new ContentValues();
args.put(KEY_LOCATION, location);
return mDB.update(DB_TABLE, args, "=" + rowId, null) > 0;
}
}
일단 리스트 구성하실때 ArrayAdapter<String>에다가 필요한 문자열만 넣어서 쓰고 계시기 때문에..
만약 레코드를 하나만 지운다고 했을때 레코드를 구분할 Key를 직접 참조할수 없습니다.
Position으로 그냥 한다면 물론 되는 케이스도 나올겁니다. 그러나 여러번 썼다 지웟다 해보면
바로 버그를 만들어낼수 있습니다.
제가 권장하는 것은 크게 2가지 입니다.
1. CursorAdapter를 활용...
2. CustomAdapter를 활용...
둘중에 택일하셔서 쓰셔야 할것 같습니다.



