我怎么能在我的Android列表中只添加唯一的联系人

时间:2013-09-18 12:04:36

标签: android addressbook android-inflate

所有..我已经在android中做了一个演示,我打开了地址簿的意图,在我的“onActivityResult”中我将联系人名称绑定到列表,一切顺利但问题是我想要的,如果1个联系人姓名已经准备好了添加它不应该再次添加,我的代码如下:

main.java

package com.example.mycontactpicker;

import java.util.zip.Inflater;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    public Button add;
    public TextView contact;
    public LinearLayout list;
    private static final int CONTACT_PICKER_RESULT = 1200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.add);

        list = (LinearLayout) findViewById(R.id.list);

        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, CONTACT_PICKER_RESULT);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:

                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                String orgName = "";
                String title = "";
                String emailId = "";
                String cNumber = "";
                String zipCode = "";
                if (c.moveToFirst()) {
                    // Fetch Contact Name
                    String DisplayName = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    System.out.println("==============Display name:::::::::;; "
                            + DisplayName);
                    View inflateView;
                    inflateView = LayoutInflater.from(MainActivity.this)
                            .inflate(R.layout.contact_row, null, true);
                    contact = (TextView) inflateView.findViewById(R.id.contact);
                    contact.setText(DisplayName);
                    list.addView(inflateView);

                    Toast.makeText(getApplicationContext(), DisplayName,
                            Toast.LENGTH_LONG).show();
                    String hasPhone = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    String contactId = c.getString(c
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    // long id = c.getLong(Integer.parseInt(contactId));

                    if (DisplayName.equals("") || DisplayName.equals(" ")) {
                        DisplayName = c
                                .getString(c
                                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE));
                        System.out
                                .println("=============Display name:::::::::::outer side "
                                        + DisplayName);


                    }
                }
            }
        }
    }
}

pls help frends

1 个答案:

答案 0 :(得分:1)

要检查地址簿中是否已存在联系人姓名,您可以添加...

    public boolean contactExists(String contact) {
    if (contact != null) {
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            if (contact.equalsIgnoreCase(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)))) {
                return true;
            }


        }
    }
    return false;
    }