1

在本教程中:http ://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/

他们正在使用扩展 ListActivity 的类,它们通过下一句显示结果:(this.setListAdapter(adapter);使用自定义 FileArrayAdapter)但我不能使用它,因为我想在 DialogFragment 中显示文件选择器(扩展 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
        }

}

这里是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;
       }

}

选项.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();
    }
}

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>
4

1 回答 1

0

因为你没有使用ListFragmentor ListActivity,所以你不能使用内置的 ListView 因为没有。为了访问 a ListView,您必须在您的 xml 布局中拥有 on 并在您的onCreateView()方法中实例化它。

以下是一个快速解决方案,可让您了解应该如何实施:

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
    }
}

这是您的新布局的代码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>
于 2013-10-29T17:50:30.440 回答