我已经在堆栈上查看了类似的问题,并且找不到我在路上遇到的特定问题的答案是不成功的.我会捐给任何可以给我解决方案的人.
我试图从没有扩展名的外部存储目录显示内容(.txt文件).我已导入(apache.commons.io).
ArrayAdapter mArrayAdapter;
ListView listView;
String fileNames[];
ArrayList<String> nameArray;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
this.setContentView(R.layout.activity_read_note_menu);
listView = (ListView) this.findViewById(R.id.readListView);
fileNames = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();
nameArray = new ArrayList<>();
for (String name: fileNames){
name = name.substring(0, name.indexOf("."));
nameArray.add(name);
Log.d("Files", "FileName:" + name);
}
mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, nameArray);
mArrayAdapter.sort(new Comparator<String>() {
public int compare(String li1, String li2) {
return li1.compareto(li2);
}
});
listView.setAdapter(mArrayAdapter);
listView.setonItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
String string = (String) listView.getItemAtPosition(i);
Intent intent = new Intent(ReadNoteMenu.this, ReadNote.class);
intent.putExtra(EXTRA_MESSAGE, string);
ReadNoteMenu.this.startActivity(intent);
finish();
}
});
解决方法:
String fileNames[] = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();
获得所有文件列表后,从文件名中删除扩展名并将该名称存储在数组中.
ArrayList<String> nameArray = new ArrayList<String>();
for (String name: fileNames){
name = name.substring(0, name.indexOf("."));
nameArray.add(name);
Log.d("Files", "FileName:" + name);
}
请在适配器中传递nameArray以完成任务,如下所示:
mArrayAdapter = new ArrayAdapter(this, R.layout.list_item_1, nameArray);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。