Saving a spinner position to a text file [Android] -
i'm trying save selected location of spinner in text file when click save button.
currently have. spinner works fine when try add part save spinner position (savesettings) problems lie.
i getting following errors:
error:(56, 85) error: incompatible types: string cannot converted file error:(61, 40) error: method getposition in class arrayadapter<t> cannot applied given types; required: charsequence found: no arguments reason: actual , formal argument lists differ in length t type-variable: t extends object declared in class arrayadapter
error:execution failed task ':app:compiledebugjavawithjavac'.
compilation failed; see compiler error output details.
i think problem tried combine 2 different approaches i'm still learning i'm confusing myself correct method saving selected spinner position.
public class mainactivity extends appcompatactivity { spinner spinner; arrayadapter<charsequence> adapter; string selectedserver; /** * attention: auto-generated implement app indexing api. * see https://g.co/appindexing/androidstudio more information. */ private googleapiclient client; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); spinner = (spinner) findviewbyid(r.id.spinner); adapter = arrayadapter.createfromresource(this, r.array.serverurl, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { selectedserver = parent.getitematposition(position).tostring(); } @override public void onnothingselected(adapterview<?> parent) { } }); } public void savesettings(view view) { file txtfolder = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/myfolder/"); if (!txtfolder.exists()) { txtfolder.mkdir(); } file file = new file(txtfolder, "setting.txt"); string.valueof(selectedserver.getbytes()); try { fileoutputstream fos = new fileoutputstream(file); fos.write(selectedserver.getbytes()); fos.close(); toast.maketext(getapplicationcontext(),"setting saved", toast.length_long).show(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
so looking in fixing problem. advice appreciated.
dave
this line first error:
file txtfolder = environment.getexternalstoragedirectory().getabsolutepath()+"/myfolder/";
you assigning string
value file. might wanna put value in constructor new file(string path)
:
file txtfolder = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/myfolder/");
the second error this:
string selectedserver = adapter.getposition().tostring();
i use string.valueof(adapter.getposition());
or implicit conversion ""+adapter.getposition();
.
if that's not case, need check getposition
returns (maybe it's empty).