Because you're not using ListFragment
or ListActivity
, you cannot use a built in ListView because there isn't one. In order to have access to a ListView
, you must have on in your xml layout as well as instantiate it in your onCreateView()
method.
The following is a quick fix to give you an idea of how you should implement:
public class Dialogo extends DialogFragment {
private File currentDir;
private FileArrayAdapter adapter;
private ListView list;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//you don't want to use the same layout as your list view!
View view = inflater.inflate(R.layout.new_layout, container);
//New stuff
list = (ListView)view.findViewById(R.id.your_list);
Context c = getActivity();
currentDir = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Toast.makeText(c, "Current Dir: "+currentDir.getName(), Toast.LENGTH_SHORT).show();
fill(currentDir);
return view;
} //oncreateview
private void fill(File f)
{
File[]dirs = f.listFiles();
getDialog().setTitle("Directorio actual: "+f.getName());
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();
try{
for(File ff: dirs)
{
if(ff.isDirectory())
dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
else
{
fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
}
}
}catch(Exception e)
{
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Option("..","Parent Directory",f.getParent()));
adapter = new FileArrayAdapter(getActivity(),R.layout.activity_browser,dir);
list.setAdapter(adapter); <--- No More Error
}
}
Here's the code for your new layout to the DialogFragment
new_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="fill_parent">
<ListView
android:id="@+id/your_list"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>