android - how to save previous button state on next click item in Gallery -
i'm new in android .i'm working gallary.i know how save previous button state when click on next gallery item.i want make default button state off on images , when button state select image shoud safe when click on next one.when come on previous image shoud show saving button state .i tried compare image id , button selected image id.but that's not work .how this?thanks lot feature help.
imageview = (imageview) findviewbyid(r.id.imagesportview); imageview.setimageresource(imgid[0]); sportbutton = (button) findviewbyid(r.id.sportbutton1); gallery = (gallery) findviewbyid(r.id.sportgallery); // creating addimgadapter gallery.setadapter(new addimgadapter(this)); @override public void onitemclick(adapterview<?> perent, view view, int position, long id) { // getting id position setselected(position); // deselect button on item click onimageclick(view, position); log.d(my_log, "img click"); log.d(my_log, "img id" + position); } }); } // deselect button on image click int itemposition = 1; public void onimageclick(view button, int position) { itemposition = position; if (selectedposition != position) { sportbutton.setselected(false); log.d(my_log, "selected postion " + selectedposition + " = " + "item position" + position); } else { if (selectedposition == position) { sportbutton.setselected(true); } log.d(my_log, "selected postion " + selectedposition + " = " + "item position " + position); } } // getting id current item int selectedposition = -1; private void setselected(int position) { selectedposition = position; imageview.setimageresource(imgid[selectedposition]); } public void onclickbutton(view button) { if (button.isselected()) { button.setselected(false); log.d(my_log, "click off"); } else { // on button click on select picture id button.setselected(true); if (selectedposition != -1) { log.d(my_log, "selected position " + selectedposition); //selectedimage(selectedposition); //intent intent = new intent(sport.this, favorites.class); } log.d(my_log, "click on"); } }
one way save state of previous button make use of shared preferences in android . shared preferences allow key value pairs of data stored can later retrieved . there 1 of dataaccess mechanisms in android . others being sqllite database & files .
android documentation on share preference
now coming problem again . once had save state of checkedbutton . later access again ( seems similar want )
part 1 accessing share preference values : public static final string prefs_file = "mypreferences"; public static final string prefs_name = "user_name"; public static final string prefs_choice = "user_choice"; sharedpreferences sp; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); chkchoice = (checkbox)findviewbyid(r.id.chkchoice); btnmain = (button)findviewbyid(r.id.btnmain); btnmain.setonclicklistener(this); // here access shared preference file . sp = this.getsharedpreferences(prefs_file, mode_private); // if have preference checkbox saved load else false(unchecked) chkchoice.setchecked(sp.getboolean(prefs_choice, false)); } part 2 setting share preference activity : sp = this.getsharedpreferences(prefs_file, mode_private); sharedpreferences.editor editor = sp.edit(); editor.putstring(prefs_name, txtname.gettext().tostring()); editor.putboolean(prefs_choice, chkchoice.ischecked()); editor.commit(); // close activity show data still saved finish();
the above checkbox . have adapt kind of button information want save . hope gets started .
Comments
Post a Comment