【问题标题】:Load and display Contact android加载并显示联系人android
【发布时间】:2015-10-15 13:45:32
【问题描述】:

我想问是否有人可以帮助我。 我想将我在android中的联系人列表中的联系人作为文本或列表视图加载到我的视图中。 我有一个按钮,上面写着“添加联系人”,按钮名称是 addcontacts。 我有一个 ContactView 活动,我已经设置了所有内容。 我已经编写了一些代码,但这不是我想要做的。

public class ContactView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_view);
        Button insert = (Button) findViewById(R.id.addcontact);
        insert.setOnClickListener(new View.OnClickListener() {

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

    public void insert() {
        Intent intent = new Intent(
                ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
                ContactsContract.Contacts.CONTENT_URI);
        intent.setData(Uri.parse("tel:011-9999999"));//specify your number here
        intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Google");
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL,
                "Addresse, Street Name, State/Country");
        startActivity(intent);
        Toast.makeText(this, "Lade Ansicht", Toast.LENGTH_SHORT).show();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact_menu, menu);
        return true;
    }

这是我已经拥有的。这让我可以在我的应用程序中创建联系人,但这不是应该的方式,因为我想显示已经创建的联系人。当我让用户在应用程序中创建它们时,他们将拥有双重联系人。

有人可以帮我从现有联系人中加载它们并以我的方式显示它们吗?

【问题讨论】:

    标签: android listview contacts


    【解决方案1】:

    您可以通过以下代码从手机联系人中获取号码和姓名

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            while (phones.moveToNext())
            {
            String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
            String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
            }
    

    【讨论】:

      【解决方案2】:

      你的最终代码

      公共类 ContactView 扩展 AppCompatActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_contact_view);
          Button insert = (Button) findViewById(R.id.addcontact);
          insert.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  scanContacts();
              }
          });
      }
      
      public void insert() {
          Intent intent = new Intent(
                  ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
                  ContactsContract.Contacts.CONTENT_URI);
          intent.setData(Uri.parse("tel:011-9999999"));//specify your number here
          intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Google");
          intent.putExtra(ContactsContract.Intents.Insert.POSTAL,
                  "Addresse, Street Name, State/Country");
          startActivity(intent);
          Toast.makeText(this, "Lade Ansicht", Toast.LENGTH_SHORT).show();
      }
      
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.contact_menu, menu);
          return true;
      }
      
      
      
      
      private void scanContacts() {
      
          boolean createContact = true;
      
          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                  null, null, null, null);
          if (cur.getCount() > 0) {
              while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(
                          cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                      Cursor pCur = cr.query(
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                              null,
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                              new String[]{id}, null);
      
      
                      while (pCur.moveToNext()) {
      
                          //Your condition here to check the input number is already in contact list 
                          if(your_condition){
                              createContact = false;//
                          }       
      
      
                          String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          Log.d("\n\nName: " + name , "Phone No: " + phoneNo);
                      }
                      pCur.close();
                  }
              }
          }
      
          if(createContact){
                  create();
                  }   
      }
      

      【讨论】:

      • 我需要在我的 ContactActivity 中写这个吗?我在哪里调用联系人方法?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多