java - Contacts android application development -
i wrote adding contacts android app contains 1 name field , 1 number field , save button,when enter name , number , press save button add contact contact list..it working fine..but when not entered , pressed save button saving contact "noname"..for wrote if , else condition not working..please give me solution source code:
package com.example.contacts; import java.util.arraylist; import android.app.activity; import android.content.contentprovideroperation; import android.os.bundle; import android.provider.contactscontract; import android.view.menu; import android.view.view; import android.widget.edittext; import android.widget.toast; //import android.view.menu; public class contactdetails extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_contact_details); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.contact_details, menu); return true; } public void onsave(view view) { edittext mytext=(edittext)findviewbyid(r.id.edittext1); string name=mytext.gettext().tostring(); toast.maketext(this,name,toast.length_long).show(); edittext mytext1=(edittext)findviewbyid(r.id.edittext2); string number=mytext1.gettext().tostring(); toast.maketext(this,number,toast.length_long).show(); arraylist < contentprovideroperation > ops = new arraylist < contentprovideroperation > (); ops.add(contentprovideroperation.newinsert( contactscontract.rawcontacts.content_uri) .withvalue(contactscontract.rawcontacts.account_type, null) .withvalue(contactscontract.rawcontacts.account_name, null) .build()); if (name != "") { ops.add(contentprovideroperation.newinsert( contactscontract.data.content_uri) .withvaluebackreference(contactscontract.data.raw_contact_id, 0) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type) .withvalue( contactscontract.commondatakinds.structuredname.display_name, name).build()); } else { toast.maketext(this, "enter valid name",toast.length_long).show(); return; } if (number != null) { ops.add(contentprovideroperation. newinsert(contactscontract.data.content_uri) .withvaluebackreference(contactscontract.data.raw_contact_id, 0) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.phone.content_item_type) .withvalue(contactscontract.commondatakinds.phone.number, number) .withvalue(contactscontract.commondatakinds.phone.type, contactscontract.commondatakinds.phone.type_mobile) .build()); } else { toast.maketext(this, "enter valid number",toast.length_long).show(); return; } try { getcontentresolver().applybatch(contactscontract.authority, ops); toast.maketext(this,"contact added",toast.length_long).show(); } catch (exception e) { e.printstacktrace(); toast.maketext(this, "exception: " + e.getmessage(), toast.length_short).show(); } } public void oncancel(view v) {`enter code here` edittext et1=(edittext) findviewbyid(r.id.edittext1); et1.settext(""); edittext et2=(edittext) findviewbyid(r.id.edittext2); et2.settext(""); } }
try use checking string this
if ((!name.equals("")) && name !=null ) { ops.add(contentprovideroperation.newinsert( contactscontract.data.content_uri) .withvaluebackreference(contactscontract.data.raw_contact_id, 0) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type) .withvalue( contactscontract.commondatakinds.structuredname.display_name, name).build()); } else { toast.maketext(this, "enter valid name",toast.length_long).show(); return; } if (!(number.equals("")) && number !=null) { ops.add(contentprovideroperation. newinsert(contactscontract.data.content_uri) .withvaluebackreference(contactscontract.data.raw_contact_id, 0) .withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.phone.content_item_type) .withvalue(contactscontract.commondatakinds.phone.number, number) .withvalue(contactscontract.commondatakinds.phone.type, contactscontract.commondatakinds.phone.type_mobile) .build()); } else { toast.maketext(this, "enter valid number",toast.length_long).show(); return; }
Comments
Post a Comment