java - Reading a text file dynamically on user selection in Android -
i building android application reads text files. now,i have multiple text files in sdcard . location of files /sdcard/textfile/
filenames: abc.txt def.txt ghi.txt
i want when users select 1 of file,the selected file should read. know code read single file i.e
file sdcard = environment.getexternalstoragedirectory(); file file = new file(sdcard,pathtofile); bufferedreader br = new bufferedreader(new filereader(file)); pathtofile stores path file abc.txt defined .
is there way can pass filepath file object file user selected currently,it works abc.txt have defined path in pathtofile
you can make list of items in textfile folder , save in list user can choose from.
public class directorybrowser extends listactivity { private list<string> items = null; private file currentdirectory; private arrayadapter<string> filelist; @override public void oncreate(bundle icicle) { super.oncreate(icicle); currentdirectory = new file("/sdcard/textfile"); getfiles(currentdirectory.listfiles()); } @override protected void onlistitemclick(listview l, view v, int position, long id){ int selectedrow = (int)id; currentdirectory = new file(items.get(selectedrow)); if(currentdirectory.isdirectory()){ getfiles(currentdirectory.listfiles()); }else{ //if selected file not directory. filename currentdirectory.getpath(); } } private void getfiles(file[] files){ items = new arraylist<string>(); for(file file : files){ items.add(file.getpath()); } filelist = new arrayadapter<string>(this,r.layout.list_text, items); setlistadapter(filelist); } }
Comments
Post a Comment