db를 이용해서 간단한 메모장을 구현중인데 레코드 삭제기능을 어떻게 해야 할지 모르겠네요;;
일단 추가 기능 구현은 edTitle텍스트에 문자를 입력하고 add키를 누르면 db에 저장되서 ListView에 출력되는 식으로 했구요
ListView에 출력할때 _ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) 써서 라디오 버튼이 같이 나오는 식으로 했습니다
추가 기능은 구현 됩니다
삭제기능은 출력된 문자들 중에 하나를 선택해서 delete 버튼을 누르면 해당값의 레코드가 삭제되게 하고 싶은데
어떻게 해야할지 도무지 감이 잡히지가 않네요;;
고수님들 제발 도와주세요

----------------------------------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"
android:background="#ffffff"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9999ff"
>
<EditText
android:id="@+id/edTitle"
android:layout_width="0px"
android:layout_weight="6"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="mOnClick"
android:layout_marginTop="5px"
/>
<Button
android:id="@+id/delete"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="mOnClick"
android:layout_marginTop="5px"
/>
</LinearLayout>
<ListView
android:id="@+id/ListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#9999ff"
/>
</LinearLayout>
-------------------------------------TodoList.java----------------------------------------------
package Moon.beginner;
import java.util.ArrayList;
import java.util.HashMap;
import android.R.layout;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.*;
public class TodoList {
public TodoList(Activity AActivity){
super();
ListView list;
_Activity = AActivity;
_DB = _Activity.openOrCreateDatabase("Moon2", _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_single_choice,
new String[] {"Title","SN"},
new int[] {android.R.id.text1,android.R.id.text2}
);
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 long Delete(String id){
String sql = "delete from TodoList where id=" + id; // 이 부분을 이렇게 써주는 것이 맞는지;;
_DB.execSQL(sql);
return 0;
}
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();
}
public BaseAdapter Adapter = null;
private ArrayList<HashMap<String,String>> _DataSource
= new ArrayList<HashMap<String,String>>();
private SQLiteDatabase _DB = null;
private Activity _Activity = null;
}
--------------------------------------Main.java---------------------------------------------------
package Moon.beginner;
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);
_ListView = (ListView) findViewById(R.id.ListView);
_ListView.setAdapter(_TodoList.Adapter);
_ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private TodoList _TodoList = null;
private EditText _edTitle = null;
private ListView _ListView = null;
public void mOnClick(View v) {
EditText ed = (EditText)findViewById(R.id.edTitle);
switch (v.getId()) {
case R.id.add:
_TodoList.Append(_edTitle.getText().toString());
_edTitle.setText("");
break;
case R.id.delete:
이 부분을 어떻게 써야하는지..
break;
}
}
}




해당 소스를 가지고 삭제처리를 구현해 봤는데요.
SimpleAdapter 대신에 SimpleCursorAdapter 를 이용하여 소스를 다시 작성해 보시면 어떨까요?
if (_ListView.getCount() > 0) { // 삭제할 항목이 있을때
int pos = _ListView.getCheckedItemPosition(); // 지금 선택된 위치를 가져와서
if (pos >= 0) { // 지금 선택된 항목이 있을때
_TodoList.Delete(pos); // 삭제 처리
_ListView.clearChoices(); // 삭제한 항목 선택해제 (하지 않으면 엉뚱한 항목이 선택되어 있음)
}
}
public void Delete(int pos) {
String sql = "delete from TodoList where SN=" + _DataSource.get(pos).get("SN");
_DB.execSQL(sql);
_DataSource.remove(pos);
Adapter.notifyDataSetChanged();
}