java - Android changing XML layout Changes data fields -
here's head scratcher...(at least me) have contact list displays list of contacts db. when user clicks on 1 of contacts edit activity comes up. works laid out currently, need have edit activity display last name entry before first name. thinking fields should have 1 one relationship, went ahead , moved edittext(xml) last name above first name in edit activity thinking should referenced id of edittext. after doing so, program displaying first name in last name field , vise-versa. have tried wiping user data on emulator no difference. realize 1 of uh-duh! type questions, if can point out obvious me, appreciated. code shown in now-working state:
i've removed chunks have nothing issue. having @ me! ken
xml:
<edittext android:id="@+id/contact_edit_first_name" android:inputtype="textpersonname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="@string/contact_edit_first_name" android:imeoptions="actionnext" android:background="@color/warn" > </edittext> <edittext android:id="@+id/contact_edit_last_name" android:inputtype="textpersonname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="top" android:hint="@string/contact_edit_last_name" android:imeoptions="actionnext" android:background="@color/warn" > </edittext>
this contact activity displays listview rows, , calls createcontact sends intent add, edit or delete rows.
public class contactsactivity extends listactivity implements loadermanager.loadercallbacks<cursor> { private simplecursoradapter adapter; /** called when activity first created. */ @override public void oncreate //do on create stuff -removed filldata(); registerforcontextmenu(getlistview()); button add_contact = (button) findviewbyid(r.id.add_contact_button); add_contact.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { createcontact(); } }); } // create options menu insert xml file // removed - not relevant // return true menu displayed } // when insert menu item selected, call createcontact //removed createcontact(); return true; } return super.onoptionsitemselected(item); } private void createcontact() { intent = new intent(this, contacteditactivity.class); startactivity(i); } //the onlistitemclick sends uri flags contacteditactivity //that edit rather new insert. @override protected void onresume() { super.onresume(); //starts new or restarts existing loader in manager getloadermanager().restartloader(0, null, this); } //the filldata method binds simplecursoraadapter listview. private void filldata() { //the desired columns bound: string[] = new string[] { contactsdb.column_last_name, contactsdb.column_first_name }; //the xml views data bound to: int[] = new int[] {r.id.label2, r.id.label}; // creation of loader using initloader method call. getloadermanager().initloader(0, null, this); adapter = new simplecursoradapter(this, r.layout.contact_row, null, from, to, 0); setlistadapter(adapter); } // sort names last name, first name string orderby = contactsdb.column_last_name + " collate nocase asc" + "," + contactsdb.column_first_name + " collate nocase asc" ; // creates new loader after initloader () call @override public loader<cursor> oncreateloader(int id, bundle args) { //etc } @override public void onloadfinished(loader<cursor> loader, cursor data) { adapter.swapcursor(data); //call requires min api 11 } @override public void onloaderreset(loader<cursor> loader) { // swap cursor adapter }
and finally, contact edit code source of grief...maybe not. save state doesn't map id's?
@override protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.activity_contact_edit); log.i(tag, "inside oncreate"); mcategory = (spinner) findviewbyid(r.id.category); mlastname = (edittext) findviewbyid(r.id.contact_edit_last_name); mfirstname = (edittext) findviewbyid(r.id.contact_edit_first_name); mhomephone = (edittext) findviewbyid(r.id.contact_edit_home_phone); mcellphone = (edittext) findviewbyid(r.id.contact_edit_cell_phone); //****************ect. etc. //declare buttons , set delete enabled false - removed - not pertinant bundle extras = getintent().getextras(); // check if uri new instance or saved record } // set save button check required fields, save contact , finish savebutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { if (textutils.isempty(mlastname.gettext().tostring()) || textutils.isempty(mfirstname.gettext().tostring())) { maketoast(); } else { setresult(result_ok); finish(); } } }); // set delete button delete contact , finish - removed - not pertinant private void filldata(uri uri) { // query parameter projection - list of columns return. // passing null return columns, inefficient (but used now!) // null, null , null are: selection, selection args, , sort order specific items cursor cursor = getcontentresolver().query(uri, null, null, null, null); if (cursor != null) { cursor.movetofirst(); string category = cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_category)); (int = 0; < mcategory.getcount(); i++) { string s = (string) mcategory.getitematposition(i); log.i("category", s); //////////////////////////////////////////// if (s.equalsignorecase(category)) { mcategory.setselection(i); } }; mlastname.settext(cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_last_name))); mfirstname.settext(cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_first_name))); mhomephone.settext(cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_phone_number))); mcellphone.settext(cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_cell_number))); mworkphone.settext(cursor.getstring(cursor .getcolumnindexorthrow(contactsdb.column_work_number))); mfax.settext(cursor.getstring(cursor //****************ect. etc. //close cursor } } protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); savestate(); outstate.putparcelable(whatevercontentprovider.content_item_type, contacturi); } @override protected void onpause() { super.onpause(); savestate(); } private void savestate() { string category = (string) mcategory.getselecteditem(); string somelast = mlastname.gettext().tostring().valueof(findviewbyid(r.id.contact_edit_last_name)); string lastname = mlastname.gettext().tostring(); string firstname = mfirstname.gettext().tostring(); string somefirst = mfirstname.gettext().tostring().valueof(findviewbyid(r.id.contact_edit_first_name)); string homephone = mhomephone.gettext().tostring(); string somephone = mhomephone.gettext().tostring().valueof(findviewbyid(r.id.contact_edit_home_phone)); string cellphone = mcellphone.gettext().tostring(); string workphone = mworkphone.gettext().tostring(); //****************ect. etc. //some logging used show first name field still came first //after changing order of edittexts. log.i("last name", lastname); log.i("some last", somelast); log.i("first name", firstname); log.i("some first", somefirst); log.i("home phone", homephone); log.i("some phone", somephone); // save if first name , last name entered // program save last name when user presses button text in last name if (lastname.length() == 0 || firstname.length() == 0) { return; } // contentvalues class used store set of values contentresolver can process. contentvalues values = new contentvalues(); values.put(contactsdb.column_category, category); values.put(contactsdb.column_last_name, lastname);//annie values.put(contactsdb.column_first_name, firstname); values.put(contactsdb.column_phone_number, homephone); //****************ect. etc. if (contacturi == null) { // create new contact contacturi = getcontentresolver().insert(whatevercontentprovider.content_uri, values); } else { // update existing contact getcontentresolver().update(contacturi, values, null, null); } } //make toast down here - removed - not pertinant }
have tried cleaning project (regenerating de r). also, try restarting ide.
this may seem stupid can solve issue...
Comments
Post a Comment