java - Android:使用 Perst Lite 保存数据,如何保存已删除的数据?

标签 java android listview persistence persist

我有一个应用程序,它有一个在 strings.xml 中声明为字符串数组的电影 ListView 。 ListView的每行有3个元素对应3个string-array,分别是Title、Gross和Date。用户可以通过单击菜单中的添加按钮来添加电影,然后他/她将被发送到带有 3 个 EditText 的第二个屏幕,他/她可以在其中添加他/她的新电影的详细信息。同样,用户也可以删除和编辑条目。

我正在使用 PerstLite 作为在我的代码中持久保存数据更改的方法,但是,我只能执行“添加”功能。这是我的代码:

我的OnCreate:

   String databasePath = getAbsolutePath("movies.dbs");

    // open the database
    db.open(databasePath, 40 * 1024);

    // check if a root object is present in this file
    Index<Lab8_082588FetchDetails> root = (Index<Lab8_082588FetchDetails>) db
            .getRoot();
    if (root == null) {
        // Root is not yet defined: storage is not initialized
        root = (Index) db.createIndex(String.class, false);
        String[] titleList = getResources().getStringArray(
                R.array.title_array);
        String[] grossList = getResources().getStringArray(
                R.array.gross_array);
        String[] dateList = getResources().getStringArray(
                R.array.date_array);
        for (int i = 0; i < titleList.length; i++) {
            Lab8_082588FetchDetails item1 = new Lab8_082588FetchDetails();
            item1.setTitle(titleList[i]);
            item1.setDate(dateList[i]);
            item1.setGross(grossList[i]);
            root.put(item1.getTitle(), item1);
            db.setRoot(root);
        }

    }

    String filter = "";
    ArrayList<Lab8_082588FetchDetails> items = root.getPrefixList(filter);
    results = new ArrayList<Lab8_082588FetchDetails>();

    for (int i = 0; i < items.size(); i++) {
        Lab8_082588FetchDetails sr = new Lab8_082588FetchDetails();
        sr.setTitle(items.get(i).getTitle());
        sr.setGross(items.get(i).getGross());
        sr.setDate(items.get(i).getDate());
        results.add(sr);
    }

    adapter = new SampleCustomAdapter(results);
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    registerForContextMenu(lv);
}

我在 OnActivitiesResult 中的“添加电影”案例:

    case ADD_MOVIE:
            Lab8_082588FetchDetails newMovie = new Lab8_082588FetchDetails();
            IgnoreCaseComparator ignoreCaseAdd = new IgnoreCaseComparator();
            NumberFormat formatterAdd = new DecimalFormat("###,###,###");

            newMovie.setTitle(data
                    .getStringExtra(Lab8_082588Add.TITLE_STRING));
            newMovie.setGross("$"
                    + formatterAdd.format(Double.parseDouble(data
                            .getStringExtra(Lab8_082588Add.GROSS_STRING))));
            newMovie.setDate(data
                    .getStringExtra(Lab8_082588Add.DATE_STRING));
            results.add(newMovie);
            adapter.notifyDataSetChanged();
            Collections.sort(results, ignoreCaseAdd);

            Index<Lab8_082588FetchDetails> rootAdd = (Index<Lab8_082588FetchDetails>) db
                    .getRoot();
            rootAdd.put(newMovie.getTitle(), newMovie);
            db.setRoot(rootAdd);

            break;

我对 Perst 的 CustomAdapter 初始化:

   Index<Lab8_082588FetchDetails> root = (Index<Lab8_082588FetchDetails>) db
            .getRoot();
    String filter = "";
    ArrayList<Lab8_082588FetchDetails> items = root.getPrefixList(filter);

我的完成();和 AbsolutePath 函数:

       public void finish() {
    // close the DB so all changes are saved
    db.close();

    Toast.makeText(this, "Saving DB", Toast.LENGTH_SHORT).show();

    super.finish();
}

private String getAbsolutePath(String databasePath) {
    try {
        // MODE_APPEND is needed or else the file will auto-delete
        openFileOutput(databasePath, Context.MODE_APPEND).close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    databasePath = getFileStreamPath(databasePath).getAbsolutePath();

    System.out.println("Initializing: " + databasePath);
    return databasePath;
}

我的问题:我已经弄明白了

   rootAdd.put(newMovie.getTitle(), newMovie);
            db.setRoot(rootAdd);

是做add的部分,但是由于网上没有资源,也没有函数列表,所以我很难搞清楚其他对应Delete函数的函数

更新:

我也试过在 delete 中使用这一行,看起来像这样:

case R.id.contextdelete:
        int pos = info.position;
        String title = results.get(info.position).getTitle();
        results.remove(pos);
        adapter.notifyDataSetChanged();
        Index<Lab8_082588FetchDetails> rootDelete =   (Index<Lab8_082588FetchDetails>) db
                .getRoot();
        rootDelete.remove(title);
        db.setRoot(rootDelete);

然而,它给出了一些类似“Key Not Unique”的东西。

最佳答案

现在才知道。我和我的 friend 正在研究这个。我们试着把

  case R.id.contextdelete:
        int pos = info.position;
        String title = results.get(info.position).getTitle();
        results.remove(pos);
        adapter.notifyDataSetChanged();

        Index<Lab9_082588FetchDetails> rootDelete = (Index<Lab9_082588FetchDetails>) db
                .getRoot();
        rootDelete.remove(title, results.get(info.position));
        db.setRoot(rootDelete);

        return true;

.remove 函数具有对象参数和您自定义的获取数据类(在本例中为 Lab9_082588FetchDetails)。事实证明,代码中的初始 .remove 函数仅删除 Key,而不删除 Object(在本例中为内容)本身。

http://docs.huihoo.com/perst/docs/doc11/org/garret/perst/Index.html

是一个有用的网站,展示了可用于此类应用程序的各种 Perst 函数。

关于java - Android:使用 Perst Lite 保存数据,如何保存已删除的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032765/

相关文章:

java - 注解处理编译步骤

java - 具有 xmlbean 绑定(bind)的 Axis2 maven 插件生成未知符号 innerType 和 addNew()

java - Android - 如何将 TextView 值从 Activity-A 传递到适配器

java - 从 .war 文件中取回我的 java 源代码?

java - 与 Jersey 和 JSR 相关的 JAX-RS

android - 如何指定 Android 模拟器应该在我的开发机器上使用哪个网络接口(interface)?

Android后台网络错误恢复

android - 在 Android 设备上获取 WifiManager.MulticastLock - 可靠且一致?

android - 空 ListView 是如何渲染的?

wpf - 更改 ListView 中 GridView 行的背景颜色