android - Edit Text to String Not Saving What's Typed -
after extensively trying figure out why sharedpreferences blank, or never created. have concluded because edit text string isn't doing anything, yet have no idea why isn't doing anything.
here code
import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.support.v4.app.fragment; import android.text.editable; import android.text.textwatcher; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.view; import android.view.viewgroup; import android.widget.edittext; import android.widget.textview; public class friday extends fragment{ private string gband, bband, adv1band, adv2band, cband, fband; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { sethasoptionsmenu(true); view view = inflater.inflate(r.layout.friday, container, false); textview gvalue = (textview) view.findviewbyid(r.id.bandtext111111); gvalue.settext( "g band" ); edittext gedit = (edittext) view.findviewbyid(r.id.edittext111111); gband = gedit.gettext().tostring(); textview bvalue = (textview) view.findviewbyid(r.id.bandtext222222); bvalue.settext( "b band" ); edittext bedit = (edittext) view.findviewbyid(r.id.edittext222222); bband = bedit.gettext().tostring(); textview adv1value = (textview) view.findviewbyid(r.id.bandtext333333); adv1value.settext( "advisory 1" ); edittext adv1edit = (edittext) view.findviewbyid(r.id.edittext333333); adv1band = adv1edit.gettext().tostring(); textview adv2value = (textview) view.findviewbyid(r.id.bandtext444444); adv2value.settext( "advisory 2" ); edittext adv2edit = (edittext) view.findviewbyid(r.id.edittext444444); adv2band = adv2edit.gettext().tostring(); textview cvalue = (textview) view.findviewbyid(r.id.bandtext555555); cvalue.settext( "c band" ); edittext cedit = (edittext) view.findviewbyid(r.id.edittext555555); cband = cedit.gettext().tostring(); textview fvalue = (textview) view.findviewbyid(r.id.bandtext666666); fvalue.settext( "f band" ); edittext fedit = (edittext) view.findviewbyid(r.id.edittext666666); fband = fedit.gettext().tostring(); return view; } public class edittextwatcher implements textwatcher { private final textview target; private edittextwatcher(textview target) { this.target = target; } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void aftertextchanged(editable s) { this.target.settext(s); } public void oncreateoptionsmenu( menu menu, menuinflater inflater) { inflater.inflate(r.menu.android_apply, menu); } } public boolean onoptionsitemselected(android.view.menuitem item) { // handle presses on action bar items switch (item.getitemid()) { case r.id.apply: sharedpreferences sharedpref = getactivity().getsharedpreferences("schedule",context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putstring("g_friday", gband); editor.putstring("b_friday", bband); editor.putstring("adv1_friday", adv1band); editor.putstring("adv2_friday", adv2band); editor.putstring("c_friday", cband); editor.putstring("f_friday", fband); editor.commit(); intent in = new intent(getactivity(), mainactivity.class); startactivity(in); return true; } return true; } }
take close @ doing there:
- you're retrieving value of
edittext
when create view. default,edittext
s should empty then. - you take empty values , store them shared preferences later on.
- when store values, not retrieving values
edittext
s -- using empty values retrieved in beginning. - your textwatcher not attached anything.
side note: java coding conventions call lower-case identifiers variables, , upper-case identifiers classes, interfaces, etc. seasoned java developer, code unreadable.
to fix it, follow pattern (shown gband):
- ditch
private string...
members , declareprivate edittext edtgband;
- in
oncreateview()
, includeedtgband = (edittext) view.findviewbyid( r.id.edittext1111111 );
- in
onoptionsitemselected
, useeditor.putstring( "g_friday", edtgband.gettext().tostring() );
Comments
Post a Comment