我有一个应用程序,它有一个在 strings.xml 中声明为字符串数组的电影 ListView。每个 ListView 行中有 3 个元素对应 3 个字符串数组,即 Title、Gross 和 Date。用户可以通过单击菜单中的添加按钮来添加电影,他/她会被发送到带有 3 个编辑文本的第二个屏幕,他/她可以在其中添加他/她的新电影的详细信息。同样,用户也可以删除和编辑条目。
我使用 PerstLite 作为在我的代码中保留数据更改的一种方式,但是,我只能执行“添加”功能。这是我的代码:
我的创建:
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);
}
我在 OnActivityResult 中的“添加电影”案例:
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);
是添加的部分,但是,由于缺乏 Internet 资源,并且没有功能列表,我在找出与删除功能相对应的其他功能时遇到了问题
更新:
我还尝试在 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”的内容。