0

我有一个列表视图,当用户单击特定项目时,会显示上下文操作模式,其中只有一个项目(应该删除它)。但是,当我单击它时,数据库没有更新(该项目仍在列表中)。谁能帮助我?

在 MainActivity 中:

final Context context = this;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> listItems = new ArrayList<String>();
ListView lv;
protected Object mActionMode;
int catPos;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    CategoryDatabase entry = new CategoryDatabase(MainActivity.this);
    entry.open();

    List<String> all = entry.getAllCategory();
    if(all.size()> 0){    
        lv = (ListView)findViewById(R.id.listView1);
        arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, all);
        lv.setAdapter(arrayAdapter);
    }else{
       Toast.makeText(MainActivity.this,"No items to display",Toast.LENGTH_LONG).show();
    } 
    entry.close();

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {

          catPos = position;

          if (mActionMode != null) {
            return false;
          }

          // Start the CAB using the ActionMode.Callback defined above
          mActionMode = MainActivity.this
              .startActionMode(mActionModeCallback);
          view.setSelected(true);
          return true;
        }
      });


}

 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_delete_cat:
               // shareCurrentItem();
                CategoryDatabase entry = new CategoryDatabase(MainActivity.this);
                entry.open();
                entry.deleteCat(catPos);

                List<String> all = entry.getAllCategory();
                lv = (ListView)findViewById(R.id.listView1);
                arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, all);
                lv.setAdapter(arrayAdapter);
                entry.close();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

在实际数据库中:

public class CategoryDatabase {

public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";

private static final String DATABASE_NAME = "DBCategory";
private static final String DATABASE_TABLE = "categoryTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public CategoryDatabase(Context c){
    ourContext = c;
}

public CategoryDatabase open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close(){
    ourHelper.close();
}

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_CATEGORY + " TEXT NOT NULL);"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public long createEntry(String category) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_CATEGORY, category);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public List<String> getAllCategory() {
    List<String> List = new ArrayList<String>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;

    Cursor cursor = ourDatabase.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            List.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }
    return List;
   }

public void deleteCat(int catPos) {
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + catPos, null);     
}  

}

4

1 回答 1

1

删除为“entry.deleteCat(catPos);” 是不正确的。如果您使用您的 catPos,它是 listView 内的位置,它不会从数据库中删除条目。您在您的数据库中创建了一个 INTEGER_PRIMARY_KEY_AUTOINCREMENT,因此该整数将自动生成,并且可能与您的位置整数不同。您需要做的是,在您的数据库中创建一个查询方法,您甚至可以从您的 DB-Entry 中获取 ID。然后你可以打电话

   entry.deleteCat(databaseId);
于 2013-06-28T12:08:24.873 回答