我必須從列表視圖中刪除保存在SQLite數據庫中的項目。我可以使用搜索「EditText」來做到這一點,但我只需要移除只點擊它們的行。 這裏是我的代碼:Android:如何刪除保存在SQLite數據庫中的列表視圖中的項目?
DBHelper:
public class EventDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "USERINFO.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE "+ Event.NewEventInfo.TABLE_NAME+"("+
Event.NewEventInfo.NAME+" TEXT,"+
Event.NewEventInfo.YEAR+" TEXT,"+
Event.NewEventInfo.MONTH+" TEXT,"+
Event.NewEventInfo.DAY+" TEXT,"+
Event.NewEventInfo.HOUR+" TEXT,"+
Event.NewEventInfo.MINUTE+" TEXT);";
public EventDbHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created/opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created...");
}
public void addInformation (String name,String year,String month,String day,String hour,String minute,SQLiteDatabase db)
{
ContentValues contentValues=new ContentValues();
contentValues.put(Event.NewEventInfo.NAME,name);
contentValues.put(Event.NewEventInfo.YEAR,year);
contentValues.put(Event.NewEventInfo.MONTH,month);
contentValues.put(Event.NewEventInfo.DAY,day);
contentValues.put(Event.NewEventInfo.HOUR,hour);
contentValues.put(Event.NewEventInfo.MINUTE,minute);
db.insert(Event.NewEventInfo.TABLE_NAME,null,contentValues);
Log.e("DATABASE OPERATIONS","One row inserted...");
}
public Cursor getInformation (SQLiteDatabase db)
{
Cursor cursor;
String[] projections = {Event.NewEventInfo.NAME, Event.NewEventInfo.YEAR, Event.NewEventInfo.MONTH,
Event.NewEventInfo.DAY, Event.NewEventInfo.HOUR, Event.NewEventInfo.MINUTE};
cursor = db.query(Event.NewEventInfo.TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}
public void deleteInformation(String name, SQLiteDatabase db)
{
String selection = Event.NewEventInfo.NAME+" LIKE ?";
String[] selection_args = {name};
db.delete(Event.NewEventInfo.TABLE_NAME,selection,selection_args);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity:
public void deleteEvent (View view)
{
search_name=et.getText().toString();
eventDbHelper= new EventDbHelper(getApplicationContext());
sqLiteDatabase= eventDbHelper.getReadableDatabase();
eventDbHelper.deleteInformation(search_name, sqLiteDatabase);
Toast.makeText(getApplicationContext(),"Event deleted",Toast.LENGTH_SHORT).show();
finish();
startActivity(getIntent());
}
ListDataAdapterActivity:
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView NAME,DAYS,HOURS,MINUTES,SECONDS;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null)
{
LayoutInflater layoutInflater =(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.list_item,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView) row.findViewById(R.id.text_name);
layoutHandler.DAYS = (TextView) row.findViewById(R.id.days_list_item);
layoutHandler.HOURS = (TextView) row.findViewById(R.id.hours_list_item);
layoutHandler.MINUTES = (TextView) row.findViewById(R.id.minutes_list_item);
//layoutHandler.SECONDS = (TextView) row.findViewById(R.id.seconds_list_item);
row.setTag(layoutHandler);
}
else{
layoutHandler =(LayoutHandler) row.getTag();
}
DataProvider dataProvider =(DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.DAYS.setText(dataProvider.getsDays());
layoutHandler.HOURS.setText(dataProvider.getsHours());
layoutHandler.MINUTES.setText(dataProvider.getsMinutes());
//layoutHandler.SECONDS.setText(dataProvider.getsSeconds());
return row;
}
}
什麼我應該編輯?
[刪除從SQLite]可能的重複(http://stackoverflow.com/questions/7633168/deleting-from-sqlite) – Vucko
如果需要我可以發佈一個示例代碼誰刪除onLongClick項目... –
發佈它請。 @AlexandreMartin –