1
我寫了添加聯繫人Android應用程序,其中包含一個名稱字段和一個號碼字段和保存按鈕,當輸入名稱和號碼,然後按保存按鈕它會將該聯繫人添加到聯繫人列表..它工作正常..但是當你沒有輸入任何東西,並按保存按鈕它保存與「noname」的聯繫..爲此我寫了如果和其他條件,但它不工作..請給我解決方案 這是我的源代碼:聯繫人android應用程序開發
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("");
}
}
if((!name.equals(「」))&& name!= null)...它不工作..如果你使用name.isEmpty()它會工作 – user2723803
謝謝你回答.. @ poovi – user2723803