storage - how to read data from sd card and display spinner of another activity in android -
i save file in sd card not retrieve data of sd card text file , display on spinner of activity.when click on read button data display on spinner of activity when click on read button txt file item display on spinner
mainactivity.java
btnwritesdfile = (button) findviewbyid(r.id.write); btnwritesdfile.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { jour = display.gettext().tostring(); // write on sd card file data in text box try { file myfile = new file("/sdcard/mysdfile.txt"); myfile.createnewfile(); fileoutputstream fout = new fileoutputstream(myfile); outputstreamwriter myoutwriter = new outputstreamwriter(fout); myoutwriter.append(display.gettext()); myoutwriter.close(); fout.close(); toast.maketext(getbasecontext(), "done writing sd 'mysdfile.txt'", toast.length_short).show(); } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show(); } } // onclick }); // btnwritesdfile btnreadsdfile = (button) findviewbyid(r.id.read); btnreadsdfile.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // write on sd card file data in text box try { file myfile = new file("/sdcard/mysdfile.txt"); fileinputstream fin = new fileinputstream(myfile); bufferedreader myreader = new bufferedreader( new inputstreamreader(fin)); string adatarow = ""; string abuffer = ""; while ((adatarow = myreader.readline()) != null) { abuffer += adatarow + "\n"; } display.settext(abuffer); myreader.close(); toast.maketext(getbasecontext(), "done reading sd 'mysdfile.txt'", toast.length_short).show(); } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show(); } } });
list.java
spinner sp1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list); sp1 = (spinner) findviewbyid(r.id.spinner1); getfilesnames(); } private void getfilesnames() { string[] filenames=getapplicationcontext().filelist(); list<string> list=new arraylist<string>(); for(int = 0; < filenames.length; i++) { list.add(filenames[i]); } arrayadapter<string> filenameadapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_1,list); sp1.setadapter(filenameadapter); }
Comments
Post a Comment