listview - Converting android file browser ListActivity example into a Fragment -
i trying figure out way use example in fragment: http://sampleprogramz.com/android/browse.php
i have changed oncreate method oncreateview method , have tried correct of errors android studio flags can't seem working.
here code:
import java.io.file; import java.io.filenamefilter; import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.list; import android.app.listactivity; import android.content.context; import android.content.intent; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.listfragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import static android.app.activity.result_ok; public class filepicker extends listfragment { public final static string extra_file_path = "file_path"; public final static string extra_show_hidden_files = "show_hidden_files"; public final static string extra_accepted_file_extensions = "accepted_file_extensions"; private final static string default_initial_directory = "/"; protected file directory; protected arraylist<file> files; protected filepickerlistadapter adapter; protected boolean showhiddenfiles = false; protected string[] acceptedfileextensions; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view emptyview = inflater.inflate(r.layout.empty_view, container, false); ((viewgroup) getlistview().getparent()).addview(emptyview); getlistview().setemptyview(emptyview); // set initial directory directory = new file(default_initial_directory); // initialize arraylist files = new arraylist<file>(); // set listadapter adapter = new filepickerlistadapter(getactivity(), files); setlistadapter(adapter); // initialize extensions array allow file extensions acceptedfileextensions = new string[] {".rc"}; // intent extras if(getactivity().getintent().hasextra(extra_file_path)) directory = new file(getactivity().getintent().getstringextra(extra_file_path)); if(getactivity().getintent().hasextra(extra_show_hidden_files)) showhiddenfiles = getactivity().getintent().getbooleanextra(extra_show_hidden_files, false); if(getactivity().getintent().hasextra(extra_accepted_file_extensions)) { arraylist<string> collection = getactivity().getintent().getstringarraylistextra(extra_accepted_file_extensions); acceptedfileextensions = (string[]) collection.toarray(new string[collection.size()]); } return emptyview; } @override public void onresume() { refreshfileslist(); super.onresume(); } protected void refreshfileslist() { files.clear(); extensionfilenamefilter filter = new extensionfilenamefilter(acceptedfileextensions); file[] files = directory.listfiles(filter); if(files != null && files.length > 0) { for(file f : files) { if(f.ishidden() && !showhiddenfiles) { continue; } files.add(f); } collections.sort(files, new filecomparator()); } adapter.notifydatasetchanged(); } public void onlistitemclick(listview l, view v, int position, long id) { file newfile = (file)l.getitematposition(position); if(newfile.isfile()) { intent = new intent(); extra.putextra(extra_file_path, newfile.getabsolutepath()); getactivity().setresult(result_ok, extra); getactivity().finish(); } else { directory = newfile; refreshfileslist(); } super.onlistitemclick(l, v, position, id); } private class filepickerlistadapter extends arrayadapter<file> { private list<file> mobjects; public filepickerlistadapter(context context, list<file> objects) { super(context, r.layout.list_item, android.r.id.text1, objects); mobjects = objects; } @override public view getview(int position, view convertview, viewgroup parent) { view row = null; if(convertview == null) { layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.list_item, parent, false); } else row = convertview; file object = mobjects.get(position); imageview imageview = (imageview)row.findviewbyid(r.id.file_picker_image); textview textview = (textview)row.findviewbyid(r.id.file_picker_text); textview.setsingleline(true); textview.settext(object.getname()); if(object.isfile()) imageview.setimageresource(r.drawable.sound); else imageview.setimageresource(r.drawable.folder); return row; } } private class filecomparator implements comparator<file> { public int compare(file f1, file f2) { if(f1 == f2) return 0; if(f1.isdirectory() && f2.isfile()) // show directories above files return -1; if(f1.isfile() && f2.isdirectory()) // show files below directories return 1; // sort directories alphabetically return f1.getname().comparetoignorecase(f2.getname()); } } private class extensionfilenamefilter implements filenamefilter { private string[] extensions; public extensionfilenamefilter(string[] extensions) { super(); extensions = extensions; } public boolean accept(file dir, string filename) { if(new file(dir, filename).isdirectory()) { // accept directory names return true; } if(extensions != null && extensions.length > 0) { for(int = 0; < extensions.length; i++) { if(filename.endswith(extensions[i])) { // filename ends extension return true; } } // filename did not match of extensions return false; } // no extensions has been set. accept file extensions. return true; } } }
thanks.
i able working inflating new xml layout filepicker fragment,
i replaced:
view emptyview = inflater.inflate(r.layout.empty_view, container, false); ((viewgroup) getlistview().getparent()).addview(emptyview); getlistview().setemptyview(emptyview); // set initial directory directory = new file(default_initial_directory); // initialize arraylist files = new arraylist<file>(); // set listadapter adapter = new filepickerlistadapter(getactivity(), files); setlistadapter(adapter);
with:
view viewlist = inflater.inflate(r.layout.file_picker, container, false); lv = (listview) viewlist.findviewbyid(r.id.filebrowselist); // set initial directory directory = new file(string.valueof(default_initial_directory)); // initialize arraylist files = new arraylist<file>(); // set listadapter adapter = new filepickerlistadapter(getactivity(), files); lv.setadapter(adapter);
Comments
Post a Comment