我有一个应用程序,该应用程序具有在string.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;
我的Perst的CustomAdapter初始化:
Index<Lab8_082588FetchDetails> root = (Index<Lab8_082588FetchDetails>) db
.getRoot();
String filter = "";
ArrayList<Lab8_082588FetchDetails> items = root.getPrefixList(filter);
我的finish();和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);
但是,它提供了一些与“密钥不唯一”相似的东西。
最佳答案
刚刚想通了。我和我的朋友正在研究这个。我们尝试把
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;
使用具有对象参数和自定义提取数据类(在本例中为Lab9_082588FetchDetails)的.remove函数。事实证明,代码中的初始.remove函数仅删除键,而不删除对象(在这种情况下为内容)本身。
http://docs.huihoo.com/perst/docs/doc11/org/garret/perst/Index.html
是一个有用的网站,它显示了对此类应用程序有用的各种Perst功能。