android - 每次启动后清空Sugar数据库表列记录

标签 android database listview sugarorm

我有一个带有一些名称和 url 的 ListView 。在我的应用程序启动后,我检查我的本地数据库中是否有任何记录首先清除我的旧记录,然后用 ListView 数据填充表列,如果不只是插入数据...我就是这样做的:

        List<LocalProduct> allAuthors = LocalProduct.listAll(LocalProduct.class);
    if (allAuthors == null) {
        for (int allList=0;allList<adapter.getCount();allList++){
            Product my = adapter.getItem(allList);
            String offline_name = my.name.toString().trim();
            String offline_url = my.image_url.toString().trim();
            LocalProduct book = new LocalProduct(offline_name, offline_url);
            book.save();
            }
        }else {
            allAuthors.clear(); //I tried this
            allAuthors.removeAll(allAuthors); //And this
        for (int allList=0;allList<adapter.getCount();allList++){
            Product my = adapter.getItem(allList);
            String offline_name = my.name.toString().trim();
            String offline_url = my.image_url.toString().trim();
            LocalProduct book = new LocalProduct(offline_name, offline_url);
            book.save();
            }
        }

但是我的表记录在每次启动后仍然存储相同的记录.. 什么是正确的方法!?

最佳答案

在初始化本地产品类列表之前插入此行:

SugarRecord.deleteAll(LocalProduct.class);

示例:

SugarRecord.deleteAll(LocalProduct.class);
List<LocalProduct> allAuthors = LocalProduct.listAll(LocalProduct.class);

关于android - 每次启动后清空Sugar数据库表列记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777011/

相关文章:

android渲染用户手指的痕迹

android - 使用自定义 CursorAdapter 使 ListView 行可点击

在使用的项目中找不到 Android aar 文件依赖项

sql - 避免多对多表中的可空外键

sql - 无需额外安装的数据库

Android ListView高亮显示多个项目

wpf - 禁用调整 ListView 中列的大小

java - 在 BaseAdapter 中过滤对象

php - 如何在mysql中存储商品的属性?

android - 如何展开 ListView 中的一个项目而不是所有项目(不使用 ExpandListView)?