我有一个 AlertDialog 实例,我将其用作文件选择对话框。它包括分层浏览功能 - 如果从列表中选择一个目录,它应该显示该目录中的文件列表。它还包括一个“上一级”按钮,可返回到上一个文件夹。我需要一种方法来在显示对话框时更新 AlertDialog 对象的内置 ListView 的内容,而无需从其构建器重新加载对话框对象。我知道适配器存在,但我需要一种从定义的实例变量而不是外部 XML 资源加载数据的方法。我重写 onResume 方法以避免按下按钮时对话框关闭,这就是我需要运行列表更新的地方。
这是我现在在 onResume 方法内用于选择按钮的 OnClick 监听器的代码。
alertDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if(position >= 0) { String[] list = getCurrentFileList(); if(list[position].equals(NO_ITEMS_TEXT)){ return; } // If the selected file is a directory, recursively update the file list and redisplay. if(getCurrentFileRefList()[position].isDirectory()){ src = getCurrentFileRefList()[position]; parseFileList(); //todo update ListView from loaded file list }else { // If the selected item is a file, give the value to the handler and dismiss the dialog. handler.handleEvent(DialogActionEventHandler.ResultID.SUBMITTED, getCurrentFileRefList()[position]); alertDialog.dismiss(); } } } });
parseFileList();
方法用于从所选源文件中获取当前文件列表。
如有任何帮助,我们将不胜感激!
最佳答案
您应该通过调用notifyDatasetChanged通知UI更新列表 https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()
更新适配器中的数据后,必须调用 Adapter.notifyDatasetChanged。
关于java - 运行时更新 AlertDialog 的 ListView 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46870834/