文件选择里面DialogFragment(重复使用code) [英] File Chooser inside DialogFragment (re-using code)
问题描述
在本教程中:的http:/ /www.dreamin$c$c.net/forums/topic/190013-creating-simple-file-chooser/
他们正在使用扩展ListActivity类,他们表现出由于下一个句子的结果: this.setListAdapter(适配器);
(蒙山定制FileArrayAdapter),但我不能使用它,因为我想显示DialogFragment(扩展DialogFragment)内的文件选择。
我AP preciate我应该如何进行任何帮助或解释,先谢谢了。
下面是我的code:
DialogFragment:
公共类Dialogo扩展DialogFragment { 私人文件currentDir;
私人FileArrayAdapter适配器; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.activity_browser,集装箱); 上下文C = getActivity();
currentDir = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Toast.makeText(C,当前目录:+ currentDir.getName(),Toast.LENGTH_SHORT).show(); 填写(currentDir); Button按钮=(按钮)view.findViewById(R.id.Btnparavolver);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
getDialog()驳回()。
}
}); 返回视图。
} // oncreateview 私人空隙填充(文件f)
{
文件[] =迪尔斯f.listFiles();
getDialog()的setTitle(上级目录实际:+ f.getName())。
清单<期权及GT; DIR =新的ArrayList<期权及GT;();
清单<期权及GT; FLS =新的ArrayList<期权及GT;();
尝试{
对于(文件FF:迪尔斯)
{
如果(ff.isDirectory())
dir.add(新选件(ff.getName(),文件夹,ff.getAbsolutePath()));
其他
{
fls.add(新选件(ff.getName(),文件大小:+ ff.length(),ff.getAbsolutePath()));
}
}
}赶上(例外五)
{ }
Collections.sort(DIR);
Collections.sort(FLS);
dir.addAll(FLS);
如果(!f.getName()。equalsIgnoreCase(SD卡))
dir.add(0,新的选项(..,父目录,f.getParent()));
适配器=新FileArrayAdapter(getActivity(),R.layout.activity_browser,DIR); this.setListAdapter(适配器); < - 错误
}}
这里的 FileArrayAdapter:
公共类FileArrayAdapter扩展ArrayAdapter<期权及GT; { 私人语境℃;
私人诠释身份证;
私人列表<期权及GT;物品; 公共FileArrayAdapter(上下文的背景下,INT textViewResourceId,
清单<期权及GT;对象){
超(背景下,textViewResourceId,对象);
C =背景;
ID = textViewResourceId;
项目=物体;
}
公共选择的getItem(int i)以
{
返回items.get(ⅰ);
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V = convertView;
如果(V == NULL){
LayoutInflater VI =(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(ID,空);
}
最后一个选项O = items.get(位置);
如果(O!= NULL){
TextView的T1 =(TextView中)v.findViewById(R.id.TextView01);
TextView的T2 =(TextView中)v.findViewById(R.id.TextView02); 如果(T1!= NULL)
t1.setText(o.getName());
如果(T2!= NULL)
t2.setText(o.getData()); }
返回伏;
}}
Options.java:
公共类选项实现可比<期权及GT; {
私人字符串名称;
私人字符串数据;
私人字符串路径; 公共选择(字符串N,STRING D,串P)
{
名称= N;
数据= D;
路径= P;
}
公共字符串的getName()
{
返回名称;
}
公共字符串的getData()
{
返回的数据;
}
公共字符串的getPath()
{
返回路径;
}
@覆盖
公众诠释的compareTo(选件O){
如果(this.name!= NULL)
返回this.name.toLowerCase()的compareTo(o.getName()与toLowerCase()。)。
其他
抛出新抛出:IllegalArgumentException();
}
}
和 activity_browser.xml:
<?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT机器人:方向=垂直的android:layout_width =FILL_PARENT> <的TextView的android:文本=@ + ID / TextView01
机器人:ID =@ + ID / TextView01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:单线=真
机器人:文字样式=大胆
机器人:layout_marginTop =5dip
机器人:layout_marginLeft =5dip>
< / TextView的> <的TextView的android:文本=@ + ID / TextView02
机器人:ID =@ + ID / TextView02
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =10dip>
< / TextView的> <按钮
机器人:ID =@ + ID / Btnparavolver
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=#EEEEEE
机器人:layout_marginTop =15dip
机器人:文字=@字符串/回归
机器人:TEXTSIZE =14sp
机器人:文字颜色=#38B0DE/>< / LinearLayout中>
由于您没有使用 ListFragment
或 ListActivity
,你不能使用内置的ListView的,因为没有一个。为了能够获得一个的ListView
,您必须对您的XML布局,以及在你的 onCreateView()$ C初始化它$ C>方法。
以下是速战速决,给你的,你应该如何实现一个想法:
公共类Dialogo扩展DialogFragment { 私人文件currentDir;
私人FileArrayAdapter适配器;
私人ListView控件列表; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ //你不希望使用相同的布局,你的列表视图!
查看查看= inflater.inflate(R.layout.new_layout,集装箱); //新的东西
名单=(ListView控件)view.findViewById(R.id.your_list); 上下文C = getActivity();
currentDir = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Toast.makeText(C,当前目录:+ currentDir.getName(),Toast.LENGTH_SHORT).show(); 填写(currentDir); 返回视图。
} // oncreateview 私人空隙填充(文件f)
{
文件[] =迪尔斯f.listFiles();
getDialog()的setTitle(上级目录实际:+ f.getName())。
清单<期权及GT; DIR =新的ArrayList<期权及GT;();
清单<期权及GT; FLS =新的ArrayList<期权及GT;();
尝试{
对于(文件FF:迪尔斯)
{
如果(ff.isDirectory())
dir.add(新选件(ff.getName(),文件夹,ff.getAbsolutePath()));
其他
{
fls.add(新选件(ff.getName(),文件大小:+ ff.length(),ff.getAbsolutePath()));
}
}
}赶上(例外五)
{ }
Collections.sort(DIR);
Collections.sort(FLS);
dir.addAll(FLS);
如果(!f.getName()。equalsIgnoreCase(SD卡))
dir.add(0,新的选项(..,父目录,f.getParent()));
适配器=新FileArrayAdapter(getActivity(),R.layout.activity_browser,DIR); list.setAdapter(适配器); < ---没有更多的错误
}
}
这里的code为您的新布局到 DialogFragment
new_layout.xml
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:layout_width =FILL_PARENT> < ListView控件
机器人:ID =@ + ID / your_list
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent/>< / LinearLayout中>
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>
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>
这篇关于文件选择里面DialogFragment(重复使用code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!