在本教程中: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>