【发布时间】:2016-06-09 14:11:16
【问题描述】:
我必须从保存在 SQLite 数据库中的列表视图中删除一个项目。我可以使用搜索“EditText”来做到这一点,但我只需要单击它们即可删除行。 这是我的代码:
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) {
}
}
主活动:
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;
}
}
我应该编辑什么?
【问题讨论】:
-
Deleting from SQLite的可能重复
-
如果需要,我可以发布一个删除项目 onLongClick 的示例代码...
-
请张贴。 @AlexandreMartin
标签: java android database sqlite listview