如何使用OnItemLongClickListener()从SQLite创建的listview中删除项目?

时间:2016-02-01 15:48:03

标签: java android sqlite listview simplecursoradapter

我无法在使用SimpleCursorAdapter创建的列表视图中找到删除的答案 所以我将这个列表视图放在一个片段中,这里是代码

public List<tblMenu> GetChildMenusClass()
{
    var UserInfo = GetUserInfo();
    UserType = UserInfo[0].UserTypeID;          
    List<tblUserMenuMapping> MenuMapping = GetMenuIdFromUserMenuMappings();

    List<string> MenuMappingMenuId= new List<string>();
    for (int i = 0; i < MenuMapping.Count; i++)
    { 
        MenuMappingMenuId.Add(MenuMapping[i].MenuID.ToString());
    }

    List<tblRoleMenuMapping> RoleMapping = GetMenuIdFromRoleMenuMappings();
    List<string> RoleMenuId = new List<string>();
    for (int i = 0; i < RoleMapping.Count; i++)
    {
        RoleMenuId.Add(RoleMapping[i].MenuID.ToString());
    }

    var menus = DataAccess.Menus.FindByExp(Menus => ((Menus.IsDeleted == false) &&
         (DataAccess.UserMenuMappings.FindByExp(UserMenuMapping => (UserMenuMapping.UserID == Global.CurrentProfile.UserID))
         .Select(UserMenuMapping => new { MenuID = UserMenuMapping.MenuID }).Contains(new { MenuID = Menus.MenuID }) ||
          DataAccess.RoleMenuMappings.FindByExp(RoleMenuMapping => DataAccess.UserRoleMappings.FindByExp(UserRoleMapping => (UserRoleMapping.UserID == Global.CurrentProfile.UserID))
         .Select(UserRoleMapping => new { RoleID = UserRoleMapping.RoleID }).Contains(new { RoleID = RoleMenuMapping.RoleID }))
         .Select(RoleMenuMapping => new { MenuID = RoleMenuMapping.MenuID }).Contains(new { MenuID = Menus.MenuID }))))
         .Select(Menus => new tblMenu { MenuID = Menus.MenuID, MenuName = Menus.MenuName }).ToList();

    return menus;
}

谢谢。

3 个答案:

答案 0 :(得分:0)

从数据库中删除并刷新列表视图。

答案 1 :(得分:0)

您必须更新传递给适配器的列表,然后调用adapter.notifyDataSetChanged()

答案 2 :(得分:0)

使用swapCursor()方法更新SimpleCursorAdapter

中的数据
myDb.getWriteableDatabase().delete(TABLE_NAME, KEY_ID + "=?", position);
Cursor newCursor = myDb.getReadableDatabase().query(**etc.**);
dataAdapter.swapCursor(newCursor);