1

名前と番号を入力して保存ボタンを押すと、その連絡先が連絡先リストに追加されます。何かを押して保存ボタンを押すと、連絡先が「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 に答える