ArrayList sorting Android -
private list<string> mitem = null; private list<string> mpath = null; string dirpath = environment.getexternalstoragedirectory().getpath(); private void getdir() { mitem = new arraylist<string>(); mpath = new arraylist<string>(); file f = new file(dirpath); file[] files = f.listfiles(); (int = 0; < files.length; i++) { file file = files[i]; if (!file.ishidden() && file.canread()) { mpath.add(file.getpath()); if (file.isdirectory()) { // folder names mitem.add(file.getname() + "/"); } else { // file name mitem.add(file.getname()); } } } filelist = new filemanageradapter(this, mitem, mpath); listview.setadapter(filelist); listview.setonitemclicklistener(onitemclick); }
here im adding list of files , folders on path arraylist, how sort folders first , files @ last. kind of sorting technique should use in order achieve this. in advance.
update: customadapter
public class filemanageradapter extends arrayadapter<string> { private list<string> mitem; private context mcontext; public filemanageradapter(context context, list<string> item, list<string> path) { super(context, r.layout.fileadapter_list, item); this.mcontext = context; this.mitem = item; } @override public view getview(final int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater minflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.fileadapter_list, null); } textview txttitle = (textview) convertview.findviewbyid(r.id.txt); txttitle.settext(mitem.get(position)); return convertview; } }
i using below code sorting files in type sorted:
protected void sortbynmdes(){ fadapter = new folderadapter(getactivity(), r.layout.file_row, filelist); fadapter.sort(new comparator<file>() { @override public int compare(file lhs, file rhs) { // todo auto-generated method stub if(lhs.isdirectory() && !rhs.isdirectory()){ return -1; }else if(!lhs.isdirectory() && rhs.isdirectory()){ return 1; } return rhs.getname().tolowercase().compareto(lhs.getname().tolowercase()); } }); setlistadapter(fadapter); }
where filelist
arraylist of files.
Comments
Post a Comment