Question

In this tutorial : http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/

they are using a class that extends ListActivity, they show the results thanks to the next sentence: this.setListAdapter(adapter); ( whith a custom FileArrayAdapter) but I cant use it because I want to show the file chooser inside a DialogFragment (extends DialogFragment).

I appreciate any help or explanation of how I should proceed, thanks in advance.

Here is my code:

DialogFragment:

    public class Dialogo extends DialogFragment {

    private File currentDir;
    private FileArrayAdapter adapter;

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

         View view = inflater.inflate(R.layout.activity_browser, container);     

         Context c = getActivity();
         currentDir = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
         Toast.makeText(c, "Current Dir: "+currentDir.getName(), Toast.LENGTH_SHORT).show();

         fill(currentDir);

         Button button = (Button)view.findViewById(R.id.Btnparavolver);
         button.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 getDialog().dismiss();
             }
        });

         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);

             this.setListAdapter(adapter);   <-- ERROR
        }

}

here the FileArrayAdapter:

    public class FileArrayAdapter extends ArrayAdapter<Option>{

    private Context c;
    private int id;
    private List<Option>items;

    public FileArrayAdapter(Context context, int textViewResourceId,
            List<Option> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }
    public Option getItem(int i)
     {
         return items.get(i);
     }
     @Override
       public View getView(int position, View convertView, ViewGroup parent) {
               View v = convertView;
               if (v == null) {
                   LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   v = vi.inflate(id, null);
               }
               final Option o = items.get(position);
               if (o != null) {
                       TextView t1 = (TextView) v.findViewById(R.id.TextView01);
                       TextView t2 = (TextView) v.findViewById(R.id.TextView02);

                       if(t1!=null)
                        t1.setText(o.getName());
                       if(t2!=null)
                        t2.setText(o.getData());

               }
               return v;
       }

}

Options.java:

public class Option implements Comparable<Option>{
    private String name;
    private String data;
    private String path;

    public Option(String n,String d,String p)
    {
        name = n;
        data = d;
        path = p;
    }
    public String getName()
    {
        return name;
    }
    public String getData()
    {
        return data;
    }
    public String getPath()
    {
        return path;
    }
    @Override
    public int compareTo(Option o) {
        if(this.name != null)
            return this.name.toLowerCase().compareTo(o.getName().toLowerCase()); 
        else 
            throw new IllegalArgumentException();
    }
}

and activity_browser.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent">

    <TextView android:text="@+id/TextView01" 
        android:id="@+id/TextView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:singleLine="true" 
        android:textStyle="bold" 
        android:layout_marginTop="5dip" 
        android:layout_marginLeft="5dip">       
    </TextView>

    <TextView android:text="@+id/TextView02" 
        android:id="@+id/TextView02" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dip">      
    </TextView>

    <Button
        android:id="@+id/Btnparavolver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#EEEEEE"
        android:layout_marginTop="15dip"
        android:text="@string/volver"
        android:textSize="14sp"
        android:textColor="#38B0DE" />

</LinearLayout>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top