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

时间:2012-08-20 05:45:54

标签: java android listview persistence persist

我有一个应用程序,它将strings.xml中声明的电影ListView作为字符串数组。每个ListView行中有3个元素对应于3个字符串数组,即Title,Gross和Date。用户可以通过单击菜单中的添加按钮来添加电影,并且他/她被发送到具有3个EditTexts的第二屏幕,在那里他/她可以添加他/她的新电影的细节。同样,用户也可以删除和编辑条目。

我使用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;

我的CustomAdapter初始化Perst:

   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);

是否进行添加的部分是由于缺少Internet资源而没有功能列表,我在查找与删除功能相对应的其他功能时遇到问题

更新:

我也尝试在删除中使用这一行,如下所示:

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”的内容。

1 个答案:

答案 0 :(得分:1)

现在就明白了。我和我的朋友正在研究这个问题。我们尝试了

  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函数具有Object的参数和您的自定义提取数据类(在本例中为Lab9_082588FetchDetails)。事实证明,代码中的初始.remove函数仅删除Key,而不删除Object(在本例中为内容)本身。

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

是一个有用的网站,它显示了对这种应用程序有用的各种Perst函数。