我正在尝试构建一个笔记应用程序。我的问题是在主页上的列表中显示文本文件 (.txt)。该列表应显示注释的标题和内容。但我不知道如何编写自定义适配器的代码。
文件是这样保存的:
final EditText title = (EditText) findViewById(R.id.title);
final EditText note = (EditText) findViewById(R.id.note);
String head = title.getText().toString();
String body = note.getText().toString();
String fullnote = body + "\n" + head;
try {
FileOutputStream fo = openFileOutput(head + ".txt", Context.MODE_APPEND);
fo.write(fullnote.getBytes());
fo.write("\n".getBytes());
fo.write("\n".getBytes());
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在我想在 ListView 中加载这些文件:
String[] listItems = {"Item 1", "Item 2", "etc.",};
ListView lv = (ListView) findViewById(R.id.mainpage_list);
lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));
“项目 1”、“项目 2”和“等等”只是占位符。
我希望文件名是项目,文件的内容是子项目。 我以前从未使用过 ListViews 和 Adapters,所以我有点无能为力。 提前致谢。
最佳答案
如果您追求甜蜜的简单,我建议:
- 将每个笔记作为一个单独的文件。
- 将文件名设为笔记的标题(一定要清理这个,这样人们就不会在其中放置诸如“..”之类的东西来操纵路径)。
这将非常容易实现和处理。如果您需要做更多花哨的事情(例如跟踪历史记录),那么您将需要重新访问该结构。但根据您的要求,我会采用上述方法。
关于java - 不断将 "changing"文本文件放入 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19338190/