i have notebook sample project , want add "note counter" using shared preferences , each time user adds note increment counter in createnote() method. added textview show counter, counter 0 , doesnt increment creating new note! ! me please!
public class mainactivity extends listactivity { private static final int editor_activity_request = 1001; private static final int menu_delete_id = 1002; private int currentnoteid; private notesdatasource datasource; list<noteitem> noteslist; int count = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); datasource = new notesdatasource(this); refreshdisplay(); textview tv = (textview) findviewbyid(r.id.textview1); tv.settext(""+count); } private void refreshdisplay() { noteslist = datasource.findall(); arrayadapter<noteitem> adapter = new arrayadapter<noteitem>(this, r.layout.list_item_layout, noteslist); setlistadapter(adapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { if (item.getitemid() == r.id.action_create) { createnote(null); } return super.onoptionsitemselected(item); } public void createnote(view v) { noteitem note = noteitem.getnew(); intent intent = new intent(this, noteeditoractivity.class); intent.putextra(noteitem.key, note.getkey()); intent.putextra(noteitem.text, note.gettext()); startactivityforresult(intent, editor_activity_request); int defaultvalue = getpreferences(mode_private).getint("count_key", count); ++defaultvalue; getpreferences(mode_private).edit().putint("count_key", defaultvalue).commit(); count = getpreferences(mode_private).getint("count_key", count); } @override protected void onlistitemclick(listview l, view v, int position, long id) { noteitem note = noteslist.get(position); intent intent = new intent(this, noteeditoractivity.class); intent.putextra(noteitem.key, note.getkey()); intent.putextra(noteitem.text, note.gettext()); startactivityforresult(intent, editor_activity_request); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == editor_activity_request && resultcode == result_ok) { noteitem note = new noteitem(); note.setkey(data.getstringextra(noteitem.key)); note.settext(data.getstringextra(noteitem.text)); datasource.update(note); refreshdisplay(); } } }
your appreciated. thanks!
based on discussion , answer given @rahul tiwari...do following modifications in code..... first create instance textview variable
//your instance variables int count; textview tv; @override protected void oncreate(bundle savedinstancestate) { //your code... tv = (textview) findviewbyid(r.id.textview1); count = getsharedpreferences("name_for_shared_preferences", context.mode_private).getint("count_key", 0); updatetextview(); } void updatetextview(){ tv.settext(""+count); } //your code till here.... public void createnote(view v) { noteitem note = noteitem.getnew(); sharedpreferences sharedpref = getsharedpreferences("name_for_shared_preferences", context.mode_private); sharedpref.edit().putint("count_key", ++count).commit(); updatetextview() /*for intial testing commenting code block intent intent = new intent(this, noteeditoractivity.class); intent.putextra(noteitem.key, note.getkey()); intent.putextra(noteitem.text, note.gettext()); startactivityforresult(intent, editor_activity_request); */ } //your code.
then can start app multiple times , check want count value.
note: when naming shared preference files, should use name that's uniquely identifiable app, such "com.example.myapp.preference_file_key" more detail refer
Comments
Post a Comment