1

我写了添加联系人Android应用程序,其中包含一个姓名字段和一个号码字段以及一个保存按钮,当输入姓名和号码并按保存按钮时,它将将该联系人添加到联系人列表中。它工作正常..但是当你没有输入任何东西并按下保存按钮,它用“noname”保存联系人..为此我写了if和else条件,但它不起作用..请给我解决方案这是我的源代码:

 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 the menu; this adds items to the action bar if it is 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("");
         }
    }
4

1 回答 1

2

尝试使用这样的检查字符串

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;
        }   
于 2013-08-28T10:56:11.763 回答