2013-07-22 78 views
0

我有一个SQLite数据库,我正在保存用户选择的数据。这些数据将在列表视图中显示,如果您长时间点击数据,它将删除该项目。这是工作,因为我看到项目从列表视图中消失,但是当我重新启动应用程序和所有列表视图项从数据库中带回来时,所有被删除的内容都会回来。我使用这样的说法:SQLite删除没有实际删除

public void deleteAlarmEntry(int pos){ 
     Log.i("Deleting item from pos: ", String.valueOf(pos)); 
     db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "='" + pos + "'", null); 
    } 

我能看到的语句被称为在日志中。有没有更好的方法来确保语句正确执行?这里有什么不对吗?

这是我叫在MainActivity的ListView项的长按RemoveItem方法:

public void removeItem(int position) { 
    alarmItemArray.remove(position); 
    dataSource.deleteAlarmEntry(position); 
    alarmAdapter.notifyDataSetChanged(); 
} 

的dataSource.deleteAlarmEntry()调用上面数据库中删除。

此外,在应用程序启动时,我把条目进入一个临时的ArrayList,然后解析的时间来获取适配器的ArrayList像这样:

dataSource = new WeatherDataSource(this); 
    dataSource.open(); 

    ArrayList<AlarmEntry> alarmEntries = (ArrayList<AlarmEntry>) dataSource.getAllWeatherEntries(); 

    alarmItemArray = getTimeFromEntries(alarmEntries); 

    alarmAdapter = new ArrayAdapter<String>(this, 
      R.layout.activity_alarm_item, R.id.time, alarmItemArray); 

    lv = (MyListView) findViewById(R.id.listview); 
    lv.setAdapter(alarmAdapter); 

这里是数据库的getAllWeatherEntries:

public List<AlarmEntry> getAllWeatherEntries(){ 
    List<AlarmEntry> weatherEntry = new ArrayList<AlarmEntry>(); 

    Cursor cursor = db.query(MySQLHelper.TABLE_NAME, cols, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     AlarmEntry m = cursorToEntry(cursor); 
     weatherEntry.add(m); 
     Log.i("Get all weather entries", m.getTime()); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 


    return weatherEntry; 
} 
+0

在这里添加您的全logcat的,请。 – Sajmon

+0

07-22 12:55:57.683:I /从pos删除项目:(29438):8 – worm

+0

并且您是否确定该项目也从数据库中删除,而不仅仅是从列表中删除? – Sajmon

回答

0

您只是将该项目的所有内容都传递给您的查询。

OnContextMenuItemSelected您需要做类似于下面的操作;您需要更改deleteAlarmEntry的参数以接收字符串而不是int。

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
String selectedID; 
// adapter is the Adapter from creating the listview 
    selectedID = String.valueOf(adapter.getItemId(info.position)); 
    deleteAlarmEntry(selectedID); 
    } 

您还需要修改db.delete返回一个数,即每int result = db.delete()功能。 http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

编辑:

您可能需要使用OnItemLongClickListener代替OnLongClickListener

http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html

lv.setOnItemLongClickListener(new OnItemLongClickListener() { 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
      String id = String.valueOf(alarmAdapter.getItemId(position); 
removeItem(id); 
      } 
    }); 

第二个编辑的: 尝试此CursorAdapter的,我建议你用一个简单的鼠标适配器: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

String[] from = new String[]{"time"}; //enter your time column name here 
int[] to = new int[]{R.id.time}; 
Cursor cursor = db.query(MySQLHelper.TABLE_NAME, cols, null, null, null, null, null); 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.activity_alarm_item, cursor, from, to); 
lv.setAdapter(adapter); 
+0

我做了一些编辑,我已经有了这个职位,并且正在通过。 – worm

+0

你想要的位置的ID不只是位置,只传递位置将通过一切,这就是为什么logcat显示(29438):8而不仅仅是来自数据库的ID。 – adefran83

+0

啊,那么这个onContextItemSelected应该添加在哪里?在mainActivity?并调用这个呢? – worm

0

使用,

public void deleteAlarmEntry(int pos) { 
    db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "=?", new String[]{pos + ""}); 
}